The MongoDB update () and Save () methods are used to update the collection of documents. The update () method updates the existing document value, replacing the existing document with the Save () method in the file.
MongoDB Update () method
The update () method updates the existing document values.
Grammar:
The basic syntax for the update () method is as follows
>db. Collection_name. Update(selectioin_criteria, updated_data)
Example
Consider the following data MyCol collection.
{ "_ID" : ObjectId(5983548781331adf45ec5), "Title":"MongoDB Overview"}{ "_id" : objectid ( 5983548781331adf45ec6 "title" : "NoSQL Overview" }{ "_id" : objectid5983548781331adf45ec7 " Title ":" tutorials Point Overview "}
The following example will set the new title ' MongoDB Overview ' file and update its title to "New MongoDB Tutorial"
>Db.MyCol.Update({' Title ':' MongoDB Overview '},{$set:{' Title ':' New MongoDB Tutorial '}})>Db.MyCol.Find(){ "_ID" : ObjectId(5983548781331adf45ec5), "Title":"New MongoDB Tutorial"}{ "_id" : objectid (5983548781331adf45ec6 "title" : "NoSQL Overview" Span class= "pun" >}{ "_id" : objectid ( 5983548781331adf45ec7 "title" : "Yiibai Overview" }>
MongoDB default will only update a single file to update multiple you need to set the parameter ' multi ' to True
>db. MyCol. Update({' title ':' MongoDB overview '},{$set: {' title ':' New MongoDB Tutorial '}},{multi:true})
MongoDB Save () method
The Save () method replaces the existing document and the Save () method through the new document
Grammar
The basic syntax for the MongoDB Save () method is as follows:
>db. Collection_name. Save({_id:ObjectId(),new_data})
Example
The following example will replace the file with _id as ' 5983548781331adf45ec7 '
>Db.MyCol.Save( { "_ID" : ObjectId(5983548781331adf45ec7), "Title":"Yiibai New Topic", "By":"Yiibai" })>Db.MyCol.Find(){ "_ID" : ObjectId(5983548781331adf45ec5), "Title":"Yiibai New Topic", "By":"Yiibai"}{ "_id" : objectid (5983548781331adf45ec6 "title" : "NoSQL Overview" Span class= "pun" >}{ "_id" : objectid ( 5983548781331adf45ec7 "title" : "Yiibai Overview" }>
MongoDB Tutorial Eighth Lesson MongoDB Update document