December 2015
Beginner to intermediate
202 pages
4h
English
When you need to update many records at once, which can be the case when you need a new field added to all the existing records, use the update_many() function.
As with find_one_and_update(), we first need to create a filter for the records that we want to update.
The following code tells us how to update multiple records using PyMongo:
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client.pythonbicookbook
customers = db.customers
result = customers.update_many(
{ 'first_name': {
'$exists': True }
},
{'$currentDate': {
'updated_at': { $type: "timestamp" }
},
'$set': {
'contacted': False
}})
print(result.matched_count)In this recipe, we are ...
Read now
Unlock full access