How to update documents form collection in MongoDB

Hello programmers, here we are going to take a look at how to update documents from the collection.

We have also covered how to insert data into a collection and how to select data from a collection.

  1. How to insert data into a collection
  2. How to select data from a collection

Update method in MongoDB using Python

One can update a document by using update_one() or update_many() method. As the name suggests, the update_one() method is used to update only a single document in the collection. The update_many() is used to update all documents which meet the criteria of the query.

The documents in the course collections are as shown below.

{'_id': ObjectId('5dee3c828d178a953eec2ad7'),
'age': 21,
'author': 'Shailesh Bhimanpelli',
'course': 'MongoDB using Python',
'rating': 1}
{'_id': ObjectId('5dee451be59a39bf5e67a6f9'),
'age': 25,
'author': 'Saruque Ahamed Mollick',
'course': 'Python, Java, C++',
'rating': 5}
{'_id': ObjectId('5dee451be59a39bf5e67a6fa'),
'age': 23,
'author': 'Ben Thomas',
'course': 'HTML, CSS',
'rating': 3}
{'_id': ObjectId('5dee4556df618453e14b243c'),
'age': 25,
'author': 'Varun Thakur',
'course': 'JavaScript, Python',
'rating': 5}
{'_id': ObjectId('5def3b1b2a61b9be930647ff'),
'age': 30,
'author': 'Sorabh Pant',
'course': 'Java',
'rating': 3}
{'_id': ObjectId('5def3b1b2a61b9be93064800'),
'age': 27,
'author': 'Sahil Shah',
'course': 'jQuery',
'rating': 3}
{'_id': ObjectId('5def3b1b2a61b9be93064801'),
'age': 21,
'author': 'Joel Smith',
'course': 'Wordpress',
'rating': 2}

update_one() method in MongoDB

It will update a single document from the collection which meets the condition specified in the query.

oldValue = {'author': 'Varun Thakur'}
newValue = {'$set': {'course': 'JavaScript, Python'}}

courses.update_one(oldValue, newValue)

for i in courses.find():
    print(i)

Output

{'_id': ObjectId('5dee3c828d178a953eec2ad7'),
'age': 21,
'author': 'Shailesh Bhimanpelli',
'course': 'MongoDB using Python',
'rating': 1}
{'_id': ObjectId('5dee451be59a39bf5e67a6f9'),
'age': 25,
'author': 'Saruque Ahamed Mollick',
'course': 'Python, Java, C++',
'rating': 5}
{'_id': ObjectId('5dee451be59a39bf5e67a6fa'),
'age': 23,
'author': 'Ben Thomas',
'course': 'HTML, CSS',
'rating': 3}
{'_id': ObjectId('5dee4556df618453e14b243c'),
'age': 25,
'author': 'Varun Thakur',
'course': 'JavaScript, Python',
'rating': 5}
{'_id': ObjectId('5def3b1b2a61b9be930647ff'),
 'age': 30,
 'author': 'Sorabh Pant',
 'course': 'Java',
 'rating': 3}
{'_id': ObjectId('5def3b1b2a61b9be93064800'),
 'age': 27,
 'author': 'Sahil Shah',
 'course': 'jQuery',
 'rating': 3}
{'_id': ObjectId('5def3b1b2a61b9be93064801'),
 'age': 21,
 'author': 'Joel Smith',
 'course': 'Wordpress',
 'rating': 2}

Here, I have updated a record where authorVarun Thakur and changed its value for the course from ‘JavaScript’ to ‘JavaScript, Python’.

update_many() method

It will update all documents from the collection which meets the condition specified in the query.

oldValue = {'rating': 3}
newValue = {'$set': {'rating': 4}}

result = courses.update_many(oldValue, newValue)

for i in courses.find():
    pprint.pprint(i)

Output

{'_id': ObjectId('5dee3c828d178a953eec2ad7'),
'age': 21,
'author': 'Shailesh Bhimanpelli',
'course': 'MongoDB using Python',
'rating': 1}
{'_id': ObjectId('5dee451be59a39bf5e67a6f9'),
'age': 25,
'author': 'Saruque Ahamed Mollick',
'course': 'Python, Java, C++',
'rating': 5}
{'_id': ObjectId('5dee451be59a39bf5e67a6fa'),
'age': 23,
'author': 'Ben Thomas',
'course': 'HTML, CSS',
'rating': 4}
{'_id': ObjectId('5dee4556df618453e14b243c'),
'age': 25,
'author': 'Varun Thakur',
'course': 'JavaScript, Python',
'rating': 5}
{'_id': ObjectId('5def3b1b2a61b9be930647ff'),
'age': 30,
'author': 'Sorabh Pant',
'course': 'Java',
'rating': 4}
{'_id': ObjectId('5def3b1b2a61b9be93064800'),
'age': 27,
'author': 'Sahil Shah',
'course': 'jQuery',
'rating': 4}
{'_id': ObjectId('5def3b1b2a61b9be93064801'),
'age': 21,
'author': 'Joel Smith',
'course': 'Wordpress',
'rating': 2}

Here we have updated records who had rating: 3 we updated it to rating: 4

The entire code is as shown.

from pymongo import MongoClient

mongo = MongoClient('mongodb://localhost:27017/')

# CodeSpeedy is the name of the database
db = mongo.CodeSpeedy

# database for courses offered by CodeSpeedy i.e. courses is the name of the collection
courses = db.courses

oldValue = {'author': 'Varun Thakur'}
newValue = {'$set': {'course': 'JavaScript, Python'}}

courses.update_one(oldValue, newValue)

for i in courses.find():
    pprint(i)

oldValue = {'rating': 3}
newValue = {'$set': {'rating': 4}}

result = courses.update_many(oldValue, newValue)

for i in courses.find():
    print(i)

Leave a Reply