What is CRUD in MongoDB?
CRUD operation: CRUD operation is the technique that allows the users to create, update, read, and delete the parts of the database.
CRUD is standing from Create, Read, Update, Delete.
- Create: The Create operation is used to insert the new data into the database.
- Read: The read operation is used to query the data from the database.
- Update: The Update operation is used to edit the data from the database.
- Delete: The Delete operation is used to delete or remove data from the database.
Create Operation: Create operation can be done in two ways.
- insertOne()
- insertMany()
insertOne(): In this way, one data can be inserted into the database.
Query:
db.collection.insertOne({
name: "Asraful",
age: "26 years",
designation: “Jr. Developer”,
address: "Dhaka, Bangladesh",
})
insertMany(): In this way, multiple data can be inserted into the database.
Query:
db.collection.insertMany({
name: "Asraful",
age: "26 years",
designation: “Jr. Developer”,
address: "Dhaka, Bangladesh",
}, {
name: "Shamim",
age: "22 years",
designation: “Jr. Security Engineer”,
address: "Dhaka, Bangladesh",
}, {
name: "Naime",
age: "27 years",
designation: “Sr. Developer”,
address: "Dhaka, Bangladesh",
})
Read Operation: We use the read operation to find single or multiple data from the database.
There are two ways to find the data.
- find()
- findOne()
find(): find() operation is used to find all the data from the database. You can simply use find() to get all of them.
Query:
db.collection.find()
Output:
{ "_id" : ObjectId("5fd98ea9ce6e8850d88270b5"), "name" : "Asraful", "age" : "26 years", “designation” : “Jr. Developer”,"address" : "Dhaka, Bangladesh"}
{ "_id" : ObjectId("5fd993a2ce6e8850d88270b7"),"name" : "Shamim", "age" : "22 years", “designation” : “Security Engineer”,"address" : "Sylhet, Bangladesh"}
{ "_id" : ObjectId("5fd993f3ce6e8850d88270b8"), "name" : "Naime", "age" : "27 years", “designation” : “Sr. Developer”,"address" : "Dhaka, Bangladesh"}
db.collection.find({“address” : “Dhaka, Bangladesh”})
Output:
{ "_id" : ObjectId("5fd98ea9ce6e8850d88270b5"), "name" : "Asraful", "age" : "26 years", “designation” : “Jr. Developer”,"address" : "Dhaka, Bangladesh"}
{ "_id" : ObjectId("5fd993f3ce6e8850d88270b8"), "name" : "Naime", "age" : "27 years", “designation” : “Sr. Developer”,"address" : "Dhaka, Bangladesh"}
findOne(): findOne() operation is used to find only one data from the database.
Query:
db.collection.findOne({query}, {projection})
Output:
{ "_id" : ObjectId("5fd993a2ce6e8850d88270b7"),"name" : "Shamim", "age" : "22 years", “designation” : “Security Engineer”,"address" : "Sylhet, Bangladesh"}
Update Operations: The update operation is used to update or edit any single or multiple of data from the database.
There are 3 ways of doing this.
- updateOne()
- updateMany()
- replaceOne()
updateOne(): It is used to update one of the data from the database. The $set keyword is used to update the specific field.
Query:
db.collection.updateOne({"_id" : ObjectId("5fd98ea9ce6e8850d88270b5"}, {$set:{“age”: "27 years"}})
updateMany(): It is used to update multiple of the data from the database. The $set keyword is used to update the specific field.
Query:
db.collection.updateMany({"address" : “Dhaka, Bangladesh”}, {$set:{“designation”: "developer"}})
replaceOne(): It is used to replace one document in a collection.
Query:
db.collection.replaceOne({"name" : “Naime”}, {“name”: “Abu Bakkar”})
Output:
{ "_id" : ObjectId("5fd993f3ce6e8850d88270b8"), "name" : "Abu Bakkar"}
Delete Operations: The delete operation is used to delete or remove any single or multiple of data from the database.
There are 3 ways of doing this.
- deleteOne()
- deleteMany()
deleteOne(): It is used to delete a single document from the database.
Query:
db.collection.deleteOne({"name" : “Naime”})
deleteMany(): It is used to delete multiple documents from the database.
Query:
db.collection.deleteMany({"address" : “Dhaka, Bangladesh”})
Top comments (0)