import pymongo
client = pymongo.MongoClient()
collection = client.testdb.test
print(collection.find_one()) # 'time': 1
collection.update_one( { '_id': 0 }, { '$set': { 'time': 3 } } )
print(collection.find_one()) # 'time': 4
collection.update_one( { '_id': 0 }, { '$inc': { 'time': 1 } } )
print(collection.find_one()) # 'time': 5
MongoDB document data can be updated with the update_one
method. You can use different update operators for different update operations, e.g., $set
, $inc
, $unset
, etc.
$inc
Increments the value of the field by the specified amount.
$set
Sets the value of a field in a document.
You can use any of the two above operators in your case. Note that $inc
can be used with fields with numbers only.