Javatpoint Logo
Javatpoint Logo

MongoDB update documents

In MongoDB, update() method is used to update or modify the existing documents of a collection.

Syntax:

Example

Consider an example which has a collection name javatpoint. Insert the following documents in collection:

After successful insertion, check the documents by following query:

Output:

{ "_id" : ObjectId("56482d3e27e53d2dbc93cef8"), "course" : "java", "details" : 
{ "duration" : "6 months", "Trainer" : "Sonoo jaiswal" }, "Batch" : 
[ {"size" : "Small", "qty" : 15 }, { "size" : "Medium", "qty" : 25 } ],
 "category" : "Programming language" }

Update the existing course "java" into "android":

Check the updated document in the collection:

Output:

{ "_id" : ObjectId("56482d3e27e53d2dbc93cef8"), "course" : "android", "details" : 
{ "duration" : "6 months", "Trainer" : "Sonoo jaiswal" }, "Batch" : 
[ {"size" : "Small", "qty" : 15 }, { "size" : "Medium", "qty" : 25 } ],
 "category" : "Programming language" }




Please Share

facebook twitter google plus pinterest tumbler

Latest 4 Tutorials