MongoDB Update Document

MongoDB Update Document – We shall learnt to update MongoDB Documents based on a selection criteria using update() method.

Syntax of update() method

where

collection_nameMame of the collection in which you update document
criteria[mandatory] Criteria to select documents for update
update[mandatory] Updates to the fields for the selected documents

Options – All options below are optional.

Option[Type][DefaultValue] DefaultActionDescription
upsert[boolean][false] Does not insert a new document when no match foundIf set to true, Inserts as a new document when no match is found for the criteria.
multi[boolean][false] Updates only one document.If set to true, updates all the documents that obey the criteria.
collation[document][default] Uses binary comparison for comparing strings.You may specify language-specific rules. Refer Collation. You can specify only only language in a Collation.
WriteConcern[document][default] Refer Write Concern.Write concern describes the level of acknowledgement requested from MongoDB.

To update a MongoDB Document, follow the step by step guide below :

  1. Prepare a Criteria to select those documents for update.
    criteria = {field1:value1, field2:value2,..}
  2. Prepare Update Document.
    update = {field1:value1, field2:value2,..}
  3. [Optional] Query the Documents based on the Criteria formed to cross verify if you want to update all those selected Documents.
    db.collection.find(criteria)
  4. Prepare the options based on your use case.
  5. Run db.collection.update() method with criteria, update and options.
    db.collection.update(criteria, update, options)

 

Example to Update single MongoDB Document

Following is an example to update only one Document.

We shall use followingpeople  Collection in this Document Update Example and run the commands in Mongo Shell.

Now, prepare a criteria to select a document.

Verify the documents you select with the criteria

We shall prepare the update document as below

To update only the values you want to change, rather than mentioning all the values once again, use $set command. Next example demonstrates the usage of $set command.

Let the Options have their default value.

And execute the update command on the Collection.

MongoDB has acknowledged that "nMatched" : 1 there has been one Document that matched the Criteria, and "nModified" : 1  has modified one document.

Verify the update by querying the collection with the criteria

The details have been updated successfully.

 

Example to Update multiple MongoDB Documents

In this example, we shall update multiple mongodb documents with the help of $set command in update document and option multi set to true.

We shall use followingpeople  Collection in this Document Update Example and run the commands in Mongo Shell.

Now, prepare a criteria to select multiple MongoDB documents.

There are multiple documents selected for the criteria.

We shall prepare the update document as below, including $set command

To update only the values you want to change, rather than mentioning all the values once again, we used $set command.

We shall set multi option to true to update multiple MongoDB documents

And execute the update command on the Collection.

MongoDB has acknowledged that "nMatched" : 2 there has been two documents that matched the criteria, and "nModified" : 2  has modified all the documents that matched the criteria.

Verify the update by querying the document with the criteria

The details have been updated successfully.

 

Conclusion :

In this MongoDB Tutorial – MongoDB Update Document, we have learnt to update single or multiple MongoDB documents using update() method and $set command with the help of examples.