Open In App

MongoDB CRUD Operations

Last Updated : 27 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

CRUD operations Create, Read, Update, and Delete—are essential for interacting with databases. In MongoDB, CRUD operations allow users to perform various actions like inserting new documents, reading data, updating records, and deleting documents from collections. Mastering these operations is fundamental to working with MongoDB and building efficient applications.

In this article, we will explain each of the four core CRUD operations in MongoDB, their use cases, and provide examples to help you perform these operations effectively.

crud-mongodb create-read-update-delete-mongoBD

What are CRUD Operations in MongoDB?

Now that we know the components of the CRUD operation, let’s learn about each individual operation in MongoDB. We will know what each operation does, and the methods to perform these operations in MongoDB. We will create, read, update and delete documents from MongoDB server.

  • Create: Add new documents to a collection.
  • Read: Retrieve documents from a collection.
  • Update: Modify existing documents.
  • Delete: Remove documents from a collection.

These operations form the core functionality of interacting with MongoDB databases and are essential for managing data.

1. Create Operations

The create or insert operations are used to insert or add new documents in the collection. If a collection does not exist, then it will create a new collection in the database. We can perform, create operations using the following methods provided by the MongoDB:

Method Description
db.collection.insertOne() It is used to insert a single document in the collection.
db.collection.insertMany() It is used to insert multiple documents in the collection.
db.createCollection() It is used to create an empty collection.

Create Operations Example

Let’s look at some examples of the Create operation from CRUD in MongoDB.

Example 1:

In this example, we are inserting details of a single student in the form of document in the student collection using db.collection.insertOne() method. create operation exampleExample 2:

In this example, we are inserting details of the multiple students in the form of documents in the student collection using db.collection.insertMany() method. insertmany() method example

2. Read Operations

The Read operations are used to retrieve documents from the collection, or in other words, read operations are used to query a collection for a document. We can perform read operation using the following method provided by the MongoDB:

Method Description
db.collection.find() It is used to retrieve documents from the collection.

db.collection.findOne()

Retrieves a single document that matches the query criteria.

Note: pretty() method is used to decorate the result such that it is easy to read.

Read Operations Example

In this example, we are retrieving the details of students from the student collection using db.collection.find() method. read operation example

3. Update Operations

The update operations are used to update or modify the existing document in the collection. We can update a single document or multiple documents that match a given query. We can perform update operations using the following methods provided by the MongoDB:

Method Description
db.collection.updateOne() It is used to update a single document in the collection that satisfy the given criteria.
db.collection.updateMany() It is used to update multiple documents in the collection that satisfy the given criteria.
db.collection.replaceOne() It is used to replace single document in the collection that satisfy the given criteria.

Update Operations Example

Let’s look at some examples of the update operation from CRUD in MongoDB.

Example 1:

In this example, we are updating the age of Sumit in the student collection using db.collection.updateOne() method. update operation example Example 2:

In this example, we are updating the year of course in all the documents in the student collection using db.collection.updateMany() method. updatemany() method example

4. Delete Operations

The delete operation are used to delete or remove the documents from a collection. We can delete documents based on specific criteria or remove all documents. We can perform delete operations using the following methods provided by the MongoDB:

Method Description
db.collection.deleteOne() It is used to delete a single document from the collection that satisfy the given criteria.
db.collection.deleteMany() It is used to delete multiple documents from the collection that satisfy the given criteria.

Delete Operations Examples

Let’s look at some examples of delete operation from CRUD in MongoDB.

Example 1:

In this example, we are deleting a document from the student collection using db.collection.deleteOne() method. delete operation exampleExample 2:

In this example, we are deleting all the documents from the student collection using db.collection.deleteMany() method. deletemany() method example

Conclusion

MongoDB’s CRUD operations are essential for effectively interacting with our database. The ability to create, read, update, and delete documents allows us to manage our data efficiently and perform all the required database management tasks. In this guide, we’ve explained each CRUD operation in MongoDB, provided examples for each, and discussed how to use MongoDB’s powerful features to manipulate and manage your data.

FAQs

What are the CRUD operations in MongoDB?

CRUD operations in MongoDB refer to the four basic operations: Create, Read, Update, and Delete. These operations are used to manipulate documents within MongoDB collections.

What is CRUD operations on database?

CRUD operations on a database involve creating, reading, updating, and deleting data within a database. These operations form the foundation of interacting with database systems, including MongoDB.

What are the CRUD operations in list?

CRUD operations on a list refer to the basic actions that can be performed on the elements of the list: Create (add new items), Read (retrieve or access items), Update (modify existing items), and Delete (remove items). These operations allow users to manage and manipulate data within a list structure.



Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg
  翻译: