MongoDB update

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

A single MongoDB collection can contain hundreds and hundreds of documents. While working with a database, we always require to make changes in the existing data. This operation is known as the Update operation. The update operation should be performed very carefully. One mistake and we can have a lot of trouble. No one wants to update the wrong record.

The MongoDB update() method is used to perform the update operation. In this article, we will discuss how to use the update() method in MongoDB.

Observe the following collection.

1
2
3
4
5
{ "_id" : ObjectId("5e463e67e6f8b824961e87cc"), "name" : "Leonel Messi", "club" : "Barcelona", "country" : "Argentina" }
{ "_id" : ObjectId("5e463ea3e6f8b824961e87cd"), "name" : "Cristiano Ronaldo", "club" : "Real Madrid", "country" : "England" }
{ "_id" : ObjectId("5e463eb3e6f8b824961e87ce"), "name" : "Neymar", "club" : "PSG", "country" : "Brazil" }
{ "_id" : ObjectId("5e463ecee6f8b824961e87cf"), "name" : "Sergio Ramos", "club" : "Barcelona", "country" : "Spain" }
{ "_id" : ObjectId("5e463ef9e6f8b824961e87d0"), "name" : "Marc-Andre ter Stegen", "club" : "Barcelona", "country" : "Germany" }

The name of this collection is “myteam”. There are a couple of mistakes in this collection. We will fix these mistakes by updating the documents using the update() method.

update()

So the first change is required in the following document.

1
{ "_id" : ObjectId("5e463ecee6f8b824961e87cf"), "name" : "Sergio Ramos", "club" : "Barcelona", "country" : "Spain" }

Sergio Ramos does not play for the football club Barcelona, he plays for Real Madrid. So we need to update the club field of this document. First, we need the query.

1
{"name" : "Sergio Ramos"}

Now, the second argument should be the update. We need the $set operator for this.

1
{ $set : { "club" : "Real Madrid"}}

This will work perfectly. Let’s run the mongo shell and try this.

Image from Gyazo

Let’s verify!

Image from Gyazo

Yes! the document was updated successfully.

We updated a single field. We can also use the update() method to update multiple fields in a document. Observe the following document.

1
{ "_id" : ObjectId("5e463ea3e6f8b824961e87cd"), "name" : "Cristiano Ronaldo", "club" : "Real Madrid", "country" : "England" }

Cristiano Ronaldo plays for Juventus and he belongs to Portugal. So we need to update multiple fields in this document. Let’s see how we can do this.

1
{ $set : { "club" : "Juventus" , "country" : "Portugal"}}

This will work fine. We have to put all the fields together with the $set operator.

Image from Gyazo

Let’s verify!

Image from Gyazo

Yes! We can see that both fields are updated successfully.

Conclusion

This is how we use the update() method in MongoDB. We have to use the $set operator along with it. We can update as many documents as we want using this method. But always take care of the query. You don’t want to update a wrong document.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.