DEV Community

Asraful Islam
Asraful Islam

Posted on

What is CRUD in MongoDB?

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",
})
Enter fullscreen mode Exit fullscreen mode

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",
})
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

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"}
Enter fullscreen mode Exit fullscreen mode
db.collection.find({“address” : “Dhaka, Bangladesh”})
Enter fullscreen mode Exit fullscreen mode

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"}

Enter fullscreen mode Exit fullscreen mode

findOne(): findOne() operation is used to find only one data from the database.

Query:

db.collection.findOne({query}, {projection})
Enter fullscreen mode Exit fullscreen mode

Output:

{ "_id" : ObjectId("5fd993a2ce6e8850d88270b7"),"name" : "Shamim", "age" : "22 years", “designation” : “Security Engineer”,"address" : "Sylhet, Bangladesh"}
Enter fullscreen mode Exit fullscreen mode

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"}})
Enter fullscreen mode Exit fullscreen mode

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"}})
Enter fullscreen mode Exit fullscreen mode

replaceOne(): It is used to replace one document in a collection.

Query:

db.collection.replaceOne({"name" : “Naime”}, {“name”: “Abu Bakkar”})
Enter fullscreen mode Exit fullscreen mode

Output:

{ "_id" : ObjectId("5fd993f3ce6e8850d88270b8"), "name" : "Abu Bakkar"}
Enter fullscreen mode Exit fullscreen mode

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”})
Enter fullscreen mode Exit fullscreen mode

deleteMany(): It is used to delete multiple documents from the database.

Query:

db.collection.deleteMany({"address" : “Dhaka, Bangladesh”})
Enter fullscreen mode Exit fullscreen mode

Top comments (0)