December 2015
Beginner to intermediate
202 pages
4h
English
A common operation in every application is that of updating a single record. For that, we'll use find_one_and_update() provided by PyMongo.
The find_one_and_update() method requires a filter to determine the record to be updated.
The following code tells us how to update a single record using PyMongo:
# Find the record you want to update and save the ID
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client.pythonbicookbook
customers = db.customers
customers.find_one_and_update(
{"first_name": "Bob", "last_name": "Smith"},
{'$set': {'contacted': False,
'updated_at': datetime.datetime.utcnow()}})The first thing we do is create a filter for the ...
Read now
Unlock full access