Home MongoDB CURD Operation
Post
Cancel

MongoDB CURD Operation

A Comprehensive Guide to CRUD Operations with MongoDB

MongoDB is a NoSQL database that stores data in a document-oriented format, making it an ideal solution for modern applications that require flexible data models. One of the key features of MongoDB is its support for CRUD operations (create, read, update, delete), which provide a simple and intuitive way to manage your data. In this blog post, we will explore each of these CRUD operations in detail and provide examples to demonstrate how they work.

Create Operation

The create operation in MongoDB is used to insert new documents into a collection. To create a document, you can use the insertOne() or insertMany() methods.

Here’s an example of using insertOne() to create a new document in a collection called “users”:

1
2
3
4
5
6
7
db.users.insertOne(
   {
      "name": "John",
      "age": 30,
      "email": "john@example.com"
   }
);

In this example, we are creating a new document with three fields: name, age, and email. The insertOne() method takes a single argument, which is the document to be inserted.

If you want to insert multiple documents at once, you can use the insertMany() method. Here’s an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
db.users.insertMany(
   [
      {
         "name": "Alice",
         "age": 25,
         "email": "alice@example.com"
      },
      {
         "name": "Bob",
         "age": 35,
         "email": "bob@example.com"
      }
   ]
);

In this example, we are inserting two documents at once using the insertMany() method.

Read Operation

The read operation in MongoDB is used to retrieve documents from a collection. To retrieve documents, you can use the find() method.

Here’s an example of using find() to retrieve all documents from a collection:

1
db.users.find();

This will return all documents from the “users” collection.

If you want to retrieve specific documents based on certain criteria, you can use the find() method with a query object. Here’s an example:

1
db.users.find({ "age": { $gt: 30 } });

In this example, we are retrieving all documents from the “users” collection where the “age” field is greater than 30.

Update Operation

The update operation in MongoDB is used to modify existing documents in a collection. To update a document, you can use the updateOne() or updateMany() methods.

Here’s an example of using updateOne() to update a document in a collection:

1
2
3
4
5
6
db.users.updateOne(
   { "name": "John" },
   {
      $set: { "age": 35 }
   }
);

In this example, we are updating the “age” field of the document where the “name” field is “John”. The $set operator is used to set the new value for the “age” field.

If you want to update multiple documents at once, you can use the updateMany() method. Here’s an example:

1
2
3
4
5
6
7
db.users.updateMany(
   { "age": { $lt: 30 } },
   {
      $set: { "age": 30 }
   }
);

In this example, we are updating the “age” field of all documents where the “age” field is less than 30.

Delete Operation

The delete operation in MongoDB is used to remove documents from a collection. To delete a document, you can use the deleteOne() or deleteMany() methods.

Here’s an example of using deleteOne() to delete a document from a collection:

1
db.users.deleteOne({ "name": "John" });

In this example, we are deleting the document where the “name” field is “John”.

If you want to delete multiple documents at once, you can use the deleteMany() method. Here’s an example:

1
db.users.deleteMany({ "age": { $gt: 30 } });

In this example, we are deleting all documents where the “age” field is greater than 30.

Conclusion

In conclusion, MongoDB’s support for CRUD operations makes it an ideal database for modern applications that require flexible data models. With MongoDB, you can easily create, read, update, and delete documents in a collection using simple and intuitive methods. Whether you’re building a web application, a mobile app, or a desktop application, MongoDB provides a robust and scalable database solution that can handle a wide variety of data requirements.

In this blog post, we have covered the basic operations of CRUD with MongoDB, including creating, reading, updating, and deleting documents in a collection. We have also provided examples to demonstrate how these operations work in practice.

This post is licensed under CC BY 4.0 by the author.