Learn how to Update, Save and Remove Methods in MongoDB

0
3919

methods

In this chapter, we are going to explore update, save and remove methods in MongoDB. We are going to gain more interest as we progress further in this MongoDB tutorial series.

Update Method
In MongoDB, the ‘update ()’ and ‘save ()’ methods are used to update the document into a collection. The ‘update ()’ method is used to update the values present in the existing document whereas ‘save ()’ method replaces the entire existing document with the new document which is passed in the ‘save ()’ method.

The following is the basic syntax for ‘update ()’ method in the MongoDB.

Example
The following is an example on using the ‘update ()’ statement.

Explanation of the code

  • Here, we are using ‘update_db’ collection name of MongoDB.
  • Then, we are using the ‘save ()’ method twice without “_id” field to save two document. Here, MongoDB will assign unique object ids to these two documents automatically.
  • Next, we are using the ‘find ()’ to display the saved records.
  • After that, we are using the ‘update ()’ method to update the ‘role’ field in the document from ‘Computer Engineer’ to ‘Electronics Engineer’. The entire syntax is shown above. This is to be noted that by default, the MongoDB will always update only a single document. If we want to update multiple documents, then we need to set a parameter ‘multi’ to true within the ‘update ()’ method as shown below.

  • Once a document is updated then we are using ‘find ()’ method to retrieve all the documents along with the updated one from the MongoDB as shown below.

update-method

Save Method
In this section, we are going to discuss on how to insert a document in the MongoDB Collection. We can insert data into the MongoDB collection by using either MongoDB’s ‘insert ()’ or ‘save ()’ method. The following are the syntaxes.

1. Save Method Syntax
The following is the basic syntax for save () command in MongoDB.

Example
The following is an example on using the save () statement.

Explanation of the code

  • Here, we are using ‘demo’ collection name of MongoDB.
  • Next, we are using the ‘save ()’ method without “_id” field to save a document. Here, MongoDB will assign unique object id to document automatically.
  • Then, we are using the ‘save ()’ method with “_id” field to save a document. Here, MongoDB will save a document with this unique id and in case another document exists in database with this unique id then it will be updated or modified with this new document.
  • Once documents are saved, we have used the ‘find ()’ method to retrieve the saved documents from the MongoDB as shown below.

save-method

2. Insert Method Syntax
The following is the basic syntax for insert () command in MongoDB.

Example
The following is an example on using the insert () statement.

Explanation of the code

  • Here, we are using the default ‘test’ collection name of MongoDB.
  • We are issuing an insert command without specifying the _id parameter. In such a case MongoDB will generate a unique ObjectId for this document with three fields i.e. title, description and company.
  • _id parameter is a 12 bytes hexadecimal number unique assigned to every document in a MongoDB collection. This is how these 12 bytes are divided as follows:

Note: – We can insert multiple documents in a single query by using an array of documents inside an ‘insert ()’ command.

insert-method

Differences between Save and Insert Methods
The ‘save ()’ method works in a similar way as that of an ‘insert ()’ method when we do not specify _id in the document. If we specify a _id then the ‘save ()’ method will replace the whole data of the document containing _id (i.e. modifying the existing record) as given in ‘save ()’ method.

Remove Method
In MongoDB, the ‘remove ()’ method is used to delete or remove a document from the collection. The ‘remove ()’ method accepts two parameters i.e. the deletion criteria and just one flag as described below.

  • Deletion criteria  it is an optional parameter. When provided, it will delete a document from the collection which is matching the criteria.
  • JustOne  it is an optional parameter. When set to true or 1, then it will remove only one document from the collection.
  • The following is the basic syntax for insert () command in MongoDB.

Syntax:

Example:
The following is an example on using the remove () statement. Consider a collection which has the following three records.

Case 1: – We want to remove a selective record with ‘role: Student’ from the collection then the following will be the command as shown below.

After executing the ‘update ()’ method with criteria to remove ‘role: Student’ we can display and see that the matching criteria document has been removed from the collection.

Case 2: – There are multiple records in a collection and we just want to delete only the first record, then we need to set justOne parameter in ‘remove ()’ method as shown below.
Syntax:

Example

remove-method

Conclusion:
In this chapter, we have learnt about the practical use of update, save, and remove methods with the help of numerous examples.

LEAVE A REPLY

Please enter your comment!
Please enter your name here