MongoDB

How do I update an existing document in MongoDB

MongoDB is a NoSQL database that supports all the traditional database functionalities in an effective manner. Some important database operations include Create, Retrieve, Update and Delete. In MongoDB, users can update the existing document using a built-in update method.

The update feature in any database plays a vital role to process the data in a database. For instance, the inventory database of any store needs to be updated time by time to keep track of the list of items in that store.

In this article, we have provided a brief demonstration of update operation in MongoDB:

How update method works in MongoDB

The update method comprises several sub-methods that are used for updating documents in MongoDB. In this section, these sub-methods are described that will help in understanding the working mechanism of MongoDB update.

Update a single document: You can follow the syntax mentioned below to update a single document in a MongoDB database.

> db.collection-name.updateOne({filter}, {update}, {options})

The instances in the above syntax are described as:

{filter}: A condition that must be met to update the documents

{update}: Contains the field(s) and value(s) to be updated of a document that meets the {filter}.

Update Multiple Documents : For multiple updating in a document, you must follow the syntax provided below:

> db.collection-name.updateMany({filter}, {update}, {options})

Replace One document : This method replaces a single document that matches a condition. The syntax of this replace method is given below:

> db.collection-name.replaceOne({filter}, {replace}, {options})

The {replace} in the above syntax contains the fields(s) and value(s) to be replaced after any document matches the filter.

Update or Replace documents using update() method: This combo method can act to update as well as replace single or multiple documents. To do so, the following syntax must be followed:

Syntax

> db.collection-name.update({query}, {update}, {options})

The {query} in the above syntax has the same meaning as {filter}.

Note: The “collection-name” in all above syntaxes is user-defined and the collection always refers to a database in MongoDB. The {options} have some specific purpose to perform, like “multi” is used in the “update()” method to update multiple/single documents.

How to use the update method in MongoDB

In this section, all the above-listed methods will be explained with examples.

Update a single document in MongoDB

We will use “authors” collection to apply updateOne() method. The content inside this collection is shown below:

> db.authors.find().pretty()

Text Description automatically generated

The command written below will add a new field “Status: Promoted” where “Author-id” field matches the value “2“:

> db.authors.updateOne({"Author-id": 2}, {$set: {"Status": "Promoted"}})

Although the output confirms that one field is modified, but you can verify this update by checking the content of the “authors” collection by using the below-mentioned command:

> db.authors.find().pretty()

Text Description automatically generated

Update multiple documents in MongoDB

MongoDB allows you to update multiple documents at once. We are using “employees” collection here and the following documents reside inside this collection:

> db.employees.find().pretty()

Text Description automatically generated

Here we will add a designation to employees. The employees that have “Salary” less than “4000“, are designated as “Author“.

The command written below will add a “designation” field to all those employees that have a “Salary” value less than “4000“.

> db.employees.updateMany({"Salary": {$lt: 4000}}, {$set: {"designation": "Author"}})

Website Description automatically generated with medium confidence

Replace a single document in MongoDB

One can replace a document with the help of “replaceOne()” method of MongoDB. In this example, we are using “staff” as a collection and following content resides inside it:

> db.staff.find().pretty()

For instance, we have replaced our instructor named “Mike” with a new instructor. The command written below will help you to add the details of the instructor in place of “Mike“:

Note: As the designation is the same, so we have not replaced that field.

> db.staff.replaceOne({"name": "Mike"}, {"name": "Jack", "Salary": 300, "experience": 5})

Update or replace a document in MongoDB using update() method

The “update()” method of MongoDB is a combination of multi and single update methods. For example, if you want to update one document or multiple documents, you can use this method in both scenarios.

The “mycollection” is used as collection in this example and the documents inside it are shown below:

> db.mycollection.find().pretty()

Text Description automatically generated

Updating one document using update() method : The command given below will update those documents that have “salary” less than “5000” and a new “Allowance” field with a value “1000” is added:

> db.mycollection.update({"salary": {$lt: 5000}}, {$set: {"Allowance": 1000}})

If we look at the “mycollection” content; there are two employees whose “salary” is less than “5000” but the “update()” method has updated only one.

Updating multiple documents using update() method : By default, “update()” method only modifies the first document that matches the query condition. To update all the documents that matches the condition, you have to set the “multi” option value to “true” as we have done in the command written below:

> db.mycollection.update({"salary": {$lt: 5000}}, {$set: {"Allowance": 2000}}, {multi: true})

The query condition ($lt: 5000) is the same as in the above scenario (update one document), but we have set the “multi” value to “true“, that’s why “2” documents are modified. In “update()“, the default value of “multi” is “false“.

Conclusion

MongoDB supports a broad list of update functions that are used in specific scenarios to modify the documents in a database. In this article, we have provided deep insight on how to update existing documents in MongoDB? For this, MongoDB has four methods in its update methods list and this post will enable you to exercise all these methods to update existing documents in MongoDB. Among all four methods, the update() method is used the most because of its dual nature of updating. The MongoDB users can follow this guide to perform the updating process on documents of a MongoDB collection.

About the author

Adnan Shabbir