Back to: MongoDB Tutorials
CRUD Operations in MongoDB with Examples
In this article, I am going to discuss How to Perform CRUD Operations in MongoDB with Examples. Please read our previous article where we discussed How to Install MongoImport and MongoExport.
CRUD Operations in MongoDB
CRUD operations are:
- In CRUD, C stands for Create operation. It is used to insert a new document into the database.
- In CRUD, R stands for Read operation. It is used to query/read a document in the database.
- In CRUD, U stands for the Update operation. It is used to update an existing document in the database.
- In CRUD, D stands for Delete operation. It is used to delete a document in the database.
Create Operation in MongoDB
In MongoDB, create operation is used to add a new document to the collection. If the specified collection does not exist then it will create a new collection in the database. We are allowed to insert single as well as multiple documents in the collection. To insert a document into the database we can use any of the following methods:
- db.collectionName.insertOne(): It is used to insert a single document in the specified collection. In this method, we insert data in the form of key-value pair. Here if the document enters successfully then this method will return acknowledged = true and insertedId.
- db.collectionName.insertMany(): It is used to insert multiple documents in the specified collection. In this method, we use brackets ([]) to indicate that we are inserting multiple documents in the collection and each document is separated by a comma (,).
Example – Inserting Single Document into MongoDB
Following is the example to insert a single document into the database:
db.myEmp.insertOne({ name: “Sus”, dept: “Designing”, qualification: “B.Tech”})
Example – Inserting Multiple Documents in MongoDB
Following is the example to insert multiple documents into the MongoDB database:
db.myEmp.insertMany([
{name: “Poo”, dept: “HR”, Qualification: “MCA”},
{name: “Sonia”, dept: “HR”, Qualification: “B.Tech”}])
Read Operation in MongoDB
In MongoDB, the read operation is used to read or retrieve documents from the specified collection. Or in other words, using the read operation we are allowed to apply a query filter to get the specified document from the database. To insert a document into the database you can use the following method:
db.collectionName.find(): It is used to retrieve documents from the specified collection:
- If you use this method without a parameter, then it will retrieve all the documents from the specified collection.
- If you use this method with the parameter then, it will retrieve only those documents that fulfill the filter criteria.
Example – Retrieving All Documents in MongoDB
Following is the example to get all documents from the database:
db.myEmp.find()
Example – Retrieving Only Selective Documents in MongoDB
Following is the example to get only specified documents that pass the given filter criteria from the database:
db.myEmp.find({“dept”:”HR”})
Here this find() function will only those employees documents whose department is HR.
Update Operation in MongoDB
In MongoDB, the update operation is used to update or modify the already present documents in the specified collection. While updating a document carefully enter data because the update operation updates the document permanently. To update a document in the database we can use any of the following methods:
- db.collectionName.updateOne(): It is used to update a single document in the specified collection that matches the given criteria. Updating a field does not mean it removed the original field instead it modifies the original field with the new value. It takes two arguments: update filer and update action. Here update filter specifies which field you want to update and the update action defines how to update that field. This function will update the very first document that matches the given filter.
- db.collectionName.updateMany(): It is used to update multiple documents that match the given filter.
- db.collectionName.replaceOne(): It is used to replace the entire document. It will replace all original fields and values with new fields and values.
Suppose we have the following database:
Example – Update Single Document in MongoDB
db.myEmp.updateOne({“dept”:”HR”}, {$set:{“Qualification”: “BCA”}})
Example – Update Multiple Documents in MongoDB
db.myEmp.updateMany({“dept”:”HR”}, {$set:{“Qualification”: “BCA”}})
Example – Replace Single Document in MongoDB
db.myEmp.replaceOne({“dept”:”Designing”}, {“Qualification”: “M.Tech”})
Delete Operation in MongoDB
In MongoDB, the delete operation is used to delete the document from the specified collection. To delete a document in the database we can use any of the following methods:
- db.collectionName.deleteOne(): It is used to delete a single document that matches the given criteria. This function will delete the very first document that matches the given filter.
- db.collectionName.deleteMany(): It is used to delete multiple documents that match the given criteria.
Suppose we have the following database:
Example – Delete Single Document in MongoDB
db.myEmp.deleteOne({“dept”:”Designing”})
Example – Delete Multiple Documents in MongoDB
db.myEmp.deleteMany({“dept”:”HR”})
In the next article, I am going to discuss How to Create MongoDB Documents with Examples. In this article, I try to explain How to Perform CRUD Operations in MongoDB with Examples. I hope you enjoy this How to Perform CRUD Operation in MongoDB article.