MongoDB Update Document – Insert & Query | MongoDB CRUD

1. Objective

In our last MongoDB Tutorial, we studied MongoDB Projection. In this article, we will learn MongoDB CRUD, MongoDB Update Document, MongoDB Insert document, and MongoDB Query Document. We will learn some basic methods to Update Documents in MongoDB. In addition, we will also see some example for MongoDB Update Document.

So, let’s study MongoDB Update Document Tutorial.

MongoDB Update Document

MongoDB Update Document – Insert & Query | MongoDB CRUD

2. MongoDB CRUD

MongoDB CRUD is a short form of all this MongoDB operation:

C – Create/Insert

R – Read /Query

U – Update

D – Delete

Do You Know How MongoDB Create and Drop Collection?

3. MongoDB Update Document

There are two methods for MongoDB update document- existing.

  • Update() Method
  • Save() Method

We use update() method when we need to update the values of the existing document. While we use the save() method when we need to replace the existing document with another document passed in it as a parameter. However, update() method is also used to replace the whole document depending upon the parameter passed.
Let’s see both of the methods for MongoDB Update Document.

a. The Update() Method

Let’s first query a document to see the data it consists.

MongoDB Update Document

MongoDB Update Document – Update() Method

Let’s Know About How MongoDB Create Database 

We have seen that dataflair consist a document and have some fields and values. Let’s update this document by adding some more information.

The syntax to update a data with this method is below.

db.collection_name.update(criteria, update_data )

The $set operator is used to update a single field.

MongoDB Update Document

MongoDB Update Document – Update() Method

The $set operator adds a new field with the specified value if it doesn’t exist. For this, the field should not violate a type constraint. In case no document is matching a query, we can use {upsert: true } to create a new document. Also we can use { multi : true } to update multiple documents. By default, this is set to false.

Let’s confirm if the document has been updated or not.

MongoDB Update Document

MongoDB Update Document – Update() Method

We see that the name has been updated in the above example. Now let’s move to the next method.

Let’s Explore Advantages & Disadvantages of MongoDB

b. The save() Method

The save method is used for both MongoDB update document and MongoDB create document. It will update a document if it exists already; if it doesn’t then it will be created. Also if we don’t specify an _id field, MongoDB creates a document with an _id which contains an ObjectId value.

Let’s see it as an example.

We do not have any collection with name “Ankit” in our database. Let’s create it with save() method.

MongoDB Update Document

MongoDB Update Document – Save() Method

Let’s confirm it by querying the newly created document.

MongoDB Update Document

MongoDB Update Document – Save() Method

4. MongoDB Insert Document

Following syntax is used for MongoDB insert document operation.

db.Collection_Name.insert()

This method creates a collection if it doesn’t exist. If the collection already exists then it will insert a document into the collection. MongoDB uses JSON documents to store data.

Join DataFlair on Telegram

Let’s understand this with an example.

Use the discussed above syntax and insert some values in it. A message appears after inserting.

Read about Features of MongoDB

WriteResult({“nInserted” : 1}) 

MongoDB Update Document

MongoDB Insert Document

You can confirm whether the document has been inserted or not by writing the below syntax.

db.Collection_Name.find()
MongoDB Update Document

MongoDB Insert Document

You can also create multiples of documents in one collection. The result will show the number of inserted documents as “nInserted”: number

MongoDB Update Document

MongoDB Insert Document

Do you know What is MongoDB Limit Records?

5. MongoDB Query Document

After learning MongoDB update document and insert document, let’s move forward to MongoDB query document. There are two methods for MongoDB query document. One is the find() method, which will show the results in a non-structured way. Another one is pretty() method to show the results in a formatted way.

The syntax for both the method and the examples is below.

i. Find() Method

db.collection_name.find()
MongoDB Update Document

MongoDB Query Document – Find() method

ii. Pretty() Method

db.collection_name.find().pretty()
MongoDB Update Document

MongoDB Query Document – Pretty() method

a. MongoDB Query Documents Based on Search Criteria

When we query documents in a collection, it retrieves all the documents. We can make a query for the specific document of our use. Let’s see how we can do that.

i. AND Criteria

You can query only documents which contain two or more specific values.

For example, let’s query those only who are from “Indore” and plays “cricket”.

MongoDB Update Document

MongoDB Query Document – AND Criteria

Also, Read – MongoDB Data Modelling with Document Structure

ii. OR criteria
We can also query for the documents containing either of the specified values.
For example, let’s query the documents for either the player is from Indore or Ujjain.

MongoDB Update Document

MongoDB Query Document – OR Criteria

iii. $in Operator

This operator provides the list of values. If the document contains any of those values, you will see them in a result.

For example, let’s query the documents those contains the name “Ankit” and “Vijay”.

MongoDB Update Document

MongoDB Query Document – OR Criteria

This was all about MongoDB Update Document Tutorial. Hope you like our explanation.

6. Conclusion

Hence, in this MongoDB Tutorial, we have discussed how MongoDB Update Document. In addition, we discussed MongoDB CRUD, MongoDB insert document and MongoDB query document. We have seen that we can set the criteria for these operations. Now you may have understood the usability of these operations in MongoDB according to your use. In the Next article, we will learn the MongoDB Delete Document. Furthermore, if you have any query, feel free to ask in a comment section.

For reference

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.