In this tutorial, we are going to learn how to update MongoDB documents.

Prerequisite

For this tutorial, we need running installation of MongoDB along with the test dataset. If you have been following these tutorials, then you should have those set up already on your machine. If you are directly reading this and you need to install MongoDB and set up test database. You can visit previous tutorials to know about it.
installing MongoDB on Windows

Update MongoDB Documents

For updating any MongoDB document, we need to provide arguments. First specifies which documents to be updated and second argument for how to update that document.

Suppose we want to add country field to the city named NEW YORK which has the population more than 105000.  We can get that city by using the following query.

Now to update above document, we need to use this selector argument and then update argument. We can specify update argument using $set operator.

After running update command, we can see that we have got a WriteResult object. It gives details about previous update command

nMatched  is the number of records matched using selector argument

nUpserted is the number of records inserted into the collection

nModified is the number of records modified during last previous update query.

We can validate our update query is successful by running find query on collection.

We can see that this document has country field added. As we have discussed in earlier tutorials, we can see that MongoDB has a flexible schema and we can change it on the go.

We can also remove field using update command. For this type of queries, we need to use $unset operator.

Upsert in MongoDB documents

Let us assume that we are updating some document in MongoDB collection. However, that document is not present in the collection. Let us see what happens if we try to update such document.

We can see that find query for a city named GOTHAM returned nothing. Moreover, when we tried to update this document we got nModified as 0 telling us our updated query has not modified any document.

If the document is not present in the collection and we want to add it while updating, then we need to use upsert query. We can convert simple update command to upsert by passing a boolean value to query.

If we pass false along with update query, which is a default value, upsert will not happen. If we pass true to update query, then MongoDB will upsert new document if it is not present.

In the above query, we can see that when we pass true value for upsert parameter in the update query, MongoDB has inserted a new document named GOTHAM in the collection. It is evident by nUpserted which has value 1 for the above query. We can validate this result by using a find query as shown above.

Multi Update

We can also use the update query to modify all the documents in the collection. For doing Multi Update in MongoDB, we have to pass another boolean value to update query. If we pass true as the fourth parameter to update query, then MongoDB will do multi-update. By default this value is false that means multi-update is disabled by default.

Let us add country field to all documents in the collection.

Above query updates all documents as we are not passing any selector argument. We can see in query result that 29354 documents were matched and all were updated.

Conclusion

We have learned how to update the MongoDB document and its three types: update single document, Upsert, and multi-update. See you soon in the next tutorial. Till then Keep Learning.

I am passionate about data analytics, machine learning, and artificial intelligence. Recently I have started blogging about my experience while learning these exciting technologies.

Leave a Reply

Close Menu