Which method is used to update documents into a collection?

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.

Which method is used to update documents into a collection?

The update methods are as follows:

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

What You Will Learn:

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

Which method is used to update documents into a collection?

Figure 2: In Robo 3T

Which method is used to update documents into a collection?

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

Which method is used to update documents into a collection?

Figure 4: In Robo 3T

Which method is used to update documents into a collection?

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

Which method is used to update documents into a collection?

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

Which method is used to update documents into a collection?

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

Which method is used to update documents into a collection?

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

Which method is used to update documents into a collection?

Figure 9: Output In Robo 3T

Which method is used to update documents into a collection?

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.

Which method is used to update documents into a collection *?

MongoDB's update() and save() methods are used to update document into a collection. The update() method updates the values in the existing document while the save() method replaces the existing document with the document passed in save() method.

How do you update data in collections?

The MongoDB shell provides the following methods to update documents in a collection:.
To update a single document, use db. collection. updateOne() ..
To update multiple documents, use db. collection. updateMany() ..
To replace a document, use db. collection. replaceOne() ..

Which method is used to insert a new document into the collection?

insert() method is used to insert a new document in a collection. A document or array of documents to insert into the collection. A document expressing the write concern.

Which of the following method is used to query documents in collection?

Which of the following method is used to query documents in collections? Explanation : The find() method with no parameters returns all documents from a collection and returns all fields for the documents.