Python Data Persistence – MongoDB – Update Document
Predictably, there is an update ( ) method available to the collection object. Just as in SQL UPDATE, the $set operator assigns updated value to a specified key. Its primary usage is, as below:
Example
db.collection.update({"key":"value"}, {$set:{"key":"newvalue"}})
For example, the following statement changes the price of ‘TV’ to 50000.
> db.products.update({"Name":"TV"}, {$set:{"price":50000}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
The WriteResult () confirms the modification. You can also use Boolean operators in the update criteria. To perform updates on multiple documents, use the update many ( ) method. The following command uses the $inc operator to increment the price by 500 for all products with ProductID greater than 3.
> db.products.updateMany({"ProductID":{$gt:3}}, {$inc:{"price":5 0 0}}) { "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }