This article explains you to update the MongoDB documents using the python pymongo module.

Update MongoDB documents:

In earlier tutorials, we have seen how to create a MongoDB database and we read the documents from the Database, now we will update the document in MongoDB.

We can update the MongoDB documents in two different ways –

  • update single document using update_one()
  • and update multiple columns at a time using update_many()

update_one():

Now we’ll update the document using the update_one() method. The update_one() function takes two parameters that the first parameter is a query object and the second one is an object that defines a new value.

If the query returns more than one result, the very first one is going to be updated.
Run:

from pymongo import MongoClient
mclient = MongoClient('localhost', 27017)  # Creating a client
db = mclient['Newdatabase']
mcol = db["Employers"]

mquery = { "f_name": "Nil" }
nvalues = { "$set": { "f_name": "Nilesh" } }

mcol.update_one(mquery, nvalues)
for mrd in mcol.find():
  print(mrd)

mclient.close()

Output:

{'_id': ObjectId('6197f62a5312dd9e82102c6b'), 'f_name': 'Rob', 'l_name': 'Carpenter'}
{'_id': ObjectId('6197f7eb588c1566078fbff2'), 'f_name': 'Rob', 'l_name': 'Carpenter'}
{'_id': ObjectId('619800106f7cca5b81de7ec7'), 'f_name': 'Nilesh', 'l_name': 'Mil'}
{'_id': ObjectId('619800106f7cca5b81de7ec8'), 'f_name': 'Andy', 'l_name': 'Apple'}
{'_id': ObjectId('619800106f7cca5b81de7ec9'), 'f_name': 'AMad', 'l_name': 'Amhed'}
{'_id': 1, 'f_name': 'Nil', 'l_name': 'Mil'}
{'_id': 2, 'f_name': 'Andy', 'l_name': 'Apple'}
{'_id': 3, 'f_name': 'AMad', 'l_name': 'Amhed'}

In the above output, in the third line Nil in f_name is replaced by Nilesh. It is the first matched results hence the sixth line Nil is ignored.

update_many() method:

Now we’ll update the document using update_many() function. Its first parameter is a query object and the second is an object that defines a new value.

If the query returns many results, all the matched documents will be updated.

Run:

from pymongo import MongoClient
mclient = MongoClient('localhost', 27017)  # Creating a client
db = mclient['Newdatabase']
mcol = db["Employers"]

mquery = { "f_name": "Nil" }
nvalues = { "$set": { "f_name": "Nilesh" } }

mcol.update_many(mquery, nvalues)
for mrd in mcol.find():
  print(mrd)

mclient.close()

Output:

{'_id': ObjectId('6197f62a5312dd9e82102c6b'), 'f_name': 'Rob', 'l_name': 'Carpenter'}
{'_id': ObjectId('6197f7eb588c1566078fbff2'), 'f_name': 'Rob', 'l_name': 'Carpenter'}
{'_id': ObjectId('619800106f7cca5b81de7ec7'), 'f_name': 'Nilesh', 'l_name': 'Mil'}
{'_id': ObjectId('619800106f7cca5b81de7ec8'), 'f_name': 'Andy', 'l_name': 'Apple'}
{'_id': ObjectId('619800106f7cca5b81de7ec9'), 'f_name': 'AMad', 'l_name': 'Amhed'}
{'_id': 1, 'f_name': 'Nilesh', 'l_name': 'Mil'}
{'_id': 2, 'f_name': 'Andy', 'l_name': 'Apple'}
{'_id': 3, 'f_name': 'AMad', 'l_name': 'Amhed'}

As we can see in the above output, all Nil in f_name are replaced by Nilesh.

References:

Happy Learning 🙂