MongoDB Update and Delete Document with Examples

All about Update() and Delete() Document in MongoDB with examples:

In our previous tutorial in this MongoDB training series, we learned about the usage of arrays in MongoDB.

In this tutorial, we will learn more about the update and delete document within the MongoDB collection.

There are four MongoDB Update methods to update the record. Each method has a specific reason to be used within MongoDB.

update and delete in MongoDB

The update methods are as follows:

  • Update()
  • UpdateOne()
  • UpdateMany()
  • FindOneAndUpdate()

Update Method

The update method is used to modify an existing document within the collection.

You can modify a specific field for the whole document depending on the criteria of an update. By default, the update method is used to modify only a single record. If you want to update multiple documents, then you can set “multi: true” within the options parameter of the update method.

Syntax:

db.collection_name.update(query, update, options)

query – This is the selection criteria of the update method. If you want to update the age of a student who has a specified name within the collection then the name is the selection criteria.

update – This parameter is used to declare what you want to update within the existing record. Suppose we have three fields within the document as name, age & class.

After this, you need to update the class of the student by name. Show in the update parameter that you will pass $set with the field that you want to update. In case of an array, you have to use mongo push to update.

options – There are multiple options that we can use as an update method. But we will focus on both of them which are important to understand.

The first option is “upset”, if its value is true then it will create a new record whenever it will try to update any non-existing document. If its value is false, then it will not insert a new record whenever a non-existing document is trying to be updated.

The second option is “multi”, which is used to apply criteria on multiple documents within a collection if its default value is false. If its value is true, then only in that case it will implement the changes on all documents that are full filling the query criteria.

Update Simple Document

Let's suppose that we have the following structure of the document within the collection.

Code

db.softwaretestinghelp.find().pretty()

Figure 1: In MongoDB Shell

update In MongoDB Shell

Figure 2: In Robo 3T

update In Robo 3T

Now, we want to update the name of the student in the above document. For this purpose, we have to write the query as you can see in the below image.

Code

db.softwaretestinghelp.update({student_name:”New Name”},{$set: {student_name:”Current Name”}})

Figure 3: In MongoDB Shell

update the name of the student in mongo DB

Figure 4: In Robo 3T

update the name of the student in robo3t

When we execute these queries in the MongoDB Shell, it returned the following message in the shell as you can observe in the below image.

Figure 5: Output In MongoDB Shell

result in mongodb

The returned message of Shell is not more communicating but when you execute the same command on Robo 3T it will show you a message as how many rows get updated in the update query execution.

Figure 6: Output In Robo 3T

Output In Robo 3T

There are four MongoDB Delete methods by which we can delete a document within any collection.

Those four methods include:

  • deleteOne()
  • deleteMany()
  • findOneAndDelete()
  • remove()

Each of the above methods is used to delete or remove a document from the MongoDB collection. However, in this tutorial, we will focus only on the “deleteOne” method.

deleteOne Method

The deleteOne method is used to delete a specific document within a MongoDB collection. Here, we just have two parameters by which we get understand what to delete from the collection.

Syntax:

db.collection_name.deleteOne(query)

query – This is the selection criteria of the delete method or you can also say this as a filter. If you want to delete the age of the student which have specified name within the collection then the name is the selection criteria or filter.

Suppose we have a staff collection having staff ID and staff member name and we want to delete a record having a specific staff ID. Then for this scenario, we have to create a delete query as you can see below.

Figure 7

delete query

Now when you execute this query, any record that is matched with the query or filter gets deleted.

Code

db.staff.deleteOne({student_staff_id:2})

Figure 8: Output In MongoDB Shell

Delete Query - Output In MongoDB Shell

Figure 9: Output In Robo 3T

Delete Query - Output In Robo 3T

Conclusion

The update method is used to modify specific fields in the whole document within the collection of MongoDB. The deleteOne method is used to delete a particular document within the MongoDB collection.

Here, we have learned how we can update single or multiple documents upon particular criteria.

We also saw how we can delete a specific document within collection based on the filter. We will discuss the other three update and delete methods in our Advanced MongoDB Tutorial Series.

In our upcoming tutorial, we will learn more about object ID within any document of the MongoDB collection.

PREV Tutorial | NEXT Tutorial