In MongoDB, the Db.collection.ipdate () and Db.collection.save () methods can modify documents that already exist in the collection. The Db.collection.update () method provides additional control over the modification. For example, Db.collectoin.update () modifies a document that already has data or a set of matching query criteria. The Db.collection.save () method replaces an existing document with the same _id.
To modify multiple documents using the update () method:
By default, the update () method updates a document that satisfies the criteria. You can modify multiple documents by setting the multi option to true when calling a method. The following example modifies the Qty field for all Documents of Type field value "book"-1. The example uses $inc, which is a modified operator variable.
Copy Code code as follows:
Db.inventory.update (
{type: ' book '},
{$inc: {qty:-1}},
{Multi:true}
)
Use the Save () method to modify a document:
The Save () method replaces an existing document. Replaces a document with the Save () method by matching an existing document with the _id field. The following example completely replaces a document with _id 10 in the inventory collection:
Copy Code code as follows:
Db.inventory.save (
{
_id:10,
Type: "Misc",
Item: "Placard"
}
)