MongoDB Update Document - MongoDB

How to update document in MongoDB?

MongoDB's update() and save() methods can be used for updating document into a collection. Update() method will update the values in the existing document whereas save() method will replace the existing document with a document passed in save() method.

MongoDB Update() Method

update() method will update the values in the existing document.

Syntax

Basic syntax of update() method is as follows

Example

Consider mycol collection has below data.

Below example will set the new title 'New MongoDB Tutorial' of the documents whose title is 'MongoDB Overview'.

By default, MongoDB updates only a single document. For updating multiple documents, set a parameter 'multi' to true.

MongoDB Save() Method

save() method will replace the existing document with new document passed in the save() method.

Syntax

Basic syntax of MongoDB save() method is shown below

Example

Below example will replace the document with the _id '5983548781331adf45ec7'.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

MongoDB Topics