Python MongoDB Update_one() (original) (raw)

Last Updated : 5 Jan, 2023

MongoDB is a cross-platform document-oriented and a non relational (i.e NoSQL) database program. It is an open-source document database, that stores the data in the form of key-value pairs.
First create a database on which we perform the update_one() operation:

Python3 `

importing Mongoclient from pymongo

from pymongo import MongoClient

try: conn = MongoClient() # Making connection

except: print("Could not connect to MongoDB")

database

db = conn.database

Created or Switched to collection

names: GeeksForGeeks

collection = db.GeeksForGeeks

Creating Records:

record1 = { "appliance":"fan", "quantity":10, "rating":"3 stars", "company":"havells"} record2 = { "appliance":"cooler", "quantity":15, "rating":"4 stars", "company":"symphony"} record3 = { "appliance":"ac", "quantity":20, "rating":"5 stars", "company":"voltas"} record4 = { "appliance":"tv", "quantity":12, "rating":"3 stars", "company":"samsung"}

Inserting the Data

rec_id1 = collection.insert_one(record1) rec_id2 = collection.insert_one(record2) rec_id3 = collection.insert_one(record3) rec_id4 = collection.insert_one(record4)

Printing the data inserted

print("The data in the database is:") cursor = collection.find() for record in cursor: print(record)

`

Output :

python-mongodb-update-one-1

MongoDB Shell:

python-update-one-mongodb-1

updateOne()

It is a function by which we can update a record in a MongoDB database or Collection. This method mainly focuses on two arguments that we passed one is the query (i.e filter) object defining which document to update and the second is an object defining the new values of the document(i.e new_values) and the rest arguments are optional that we will discuss in the syntax section. This function finds the first document that matches with the query and update it with an object defining the new values of the document, i.e Updates a single document within the collection based on the filter.
Syntax:

collection.update_one(filter, new_values, upsert=False, bypass_document_validation=False, collation=None, array_filters=None, session=None)
Parameters:

Example 1: In this example, we are going to update the fan quantity from 10 to 25.

Python3 `

importing Mongoclient from pymongo

from pymongo import MongoClient

conn = MongoClient('localhost', 27017)

database

db = conn.database

Created or Switched to collection

names: GeeksForGeeks

collection = db.GeeksForGeeks

Updating fan quantity from 10 to 25.

filter = { 'appliance': 'fan' }

Values to be updated.

newvalues = { "$set": { 'quantity': 25 } }

Using update_one() method for single

updation.

collection.update_one(filter, newvalues)

Printing the updated content of the

database

cursor = collection.find() for record in cursor: print(record)

`

Output :

python-momgodb-update-one-2

MongoDB Shell:

python-mongodb-update-one-3

Example 2: In this example we are changing the tv company name from 'samsung' to 'sony' by using update_one():

Python3 `

importing Mongoclient from pymongo

from pymongo import MongoClient

conn = MongoClient('localhost', 27017)

database

db = conn.database

Created or Switched to collection

names: GeeksForGeeks

collection = db.GeeksForGeeks

Updating the tv company name from

'samsung' to 'sony'.

filter = { 'appliance': 'tv' }

Values to be updated.

newvalues = { "$set": { 'company': "sony" } }

Using update_one() method for single updation.

collection.update_one(filter, newvalues)

Printing the updated content of the database

cursor = collection.find() for record in cursor: print(record)

`

Output :

python-mongodb-update-one-5

MongoDB Shell:

python-mongodb-update-one-5

**NOTE :**The "$set" operator replaces the value of a field with the specified value. If the field does not exist, "$set" will add a new field with the specified value, provided that the new field does not violate a type constraint.