In MongoDB, update() method is used to update or modify the existing documents of a collection.
Syntax:
db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA)
Consider an example which has a collection name rookienerd. Insert the following documents in collection:
db.rookienerd.insert(
{
course: "java",
details: {
duration: "6 months",
Trainer: "Sonoo jaiswal"
},
Batch: [ { size: "Small", qty: 15 }, { size: "Medium", qty: 25 } ],
category: "Programming language"
}
)After successful insertion, check the documents by following query:
>db.rookienerd.find()
Output:
Update the existing course "java" into "android":
>db.rookienerd.update({'course':'java'},{$set:{'course':'android'}})Check the updated document in the collection:
>db.rookienerd.find()
Output:
