DEV Community

Cover image for CRUD OPERATION IS EASY PEASY IF YOU READ THIS!
Jahid Iqbal
Jahid Iqbal

Posted on

CRUD OPERATION IS EASY PEASY IF YOU READ THIS!

CRUD operation refers to creating,reading,updating and deleting documents in MongoDB.The term CRUD refers to the functions that users need in order to create and manage data.
It makes application design easier and more scalable by simplifying and facilitating it

Create Operations

Create or insert operations are used to insert or add new documents in the collection.If thereโ€™s no collection,then it will create a new collection.

Create operation follow two methods to insert documents into a collection.First method is db.collection.insertOne() and the second one is db.collection.insertMany()

  • db.collection.insertOne()

It is used to insert a single document in the collection.The document we want to insert is always written inside the insertOne function in a JSON format enclosed inside curly brackets. This is how (below example) we insert one document inside a collection.

Example

db.collection-name.insertOne(
{
person : 3,
car : 4,
}
)

  • db.collection.insertMany()

It is used to insert multiple documents in the collection.If we want to insert more than one document at a time, we have to use insertMany.

Example

db.cars.insertMany([{
name: "Rolex"
model: "2005"
},{
name: "G-shock"
model: "2018"
}])

Read Operations

Read operations are used to retrieve documents from the collection or query a collection for documents.The db.collection.find() operation will return everything from a collection if we call it without any parameters.We can also choose any filter or criteria to obtain data from a collection by using db.collection.find(query)

Example

  • db.cars.find() // no parameters

Output

{ "_id" : ObjectId("1"), "name" : "Asus laptop", "model" : "2005" }
{ "_id" : ObjectId("2"), "name" : "HP laptop", "model" : "2018" }
{ "_id" : ObjectId("3"), "name" : "Dell laptop", "2013" : "2005" }

  • db.cars.find({"model": "2005"}) // with one parameter

Output

{ "_id" : ObjectId("1"), "name" : "Asus laptop", "model" : "2005"}

Update Operations

Update operations are used to update or change the existing document in the collection.
Three ways db.collection.updateOne(), db.collection.updateMany() , db.collection.replaceOne() to update documents of a collection.

  • db.collection.updateOne()

It is used to update a single document in the collection.

Example

updateOne({"_id" : ObjectId("1")}, {$set: { "new_model" : 2020}})

Output

{"_id" : ObjectId("1"),
"model" : 2005,
"new_model" : 2020}

  • db.collection.updateMany()

It is used to update multiple documents in the collection.

Example

updateMany({"_id" : ObjectId("1")}, {$set: { "name" : "laxy"}, $set: { "new_model" : 2020}})

Output

{"_id" : ObjectId("1"),
"model" : 2005,
"new_model" : 2020
"name" : "laxy"}

  • db.collection.replaceOne()

It is used to replace a single document in the collection .

Example

{
"_id" : ObjectId("1"),
"model" : 2005
}

replaceOne({"_id" : ObjectId("1")}, { "new_model" : 2020})

Output

{ "_id" : ObjectId("1"),
"new_model" : 2020
}

Delete Operations

The delete operation is used to delete or eliminate the documents from a collection.
There are two methods to delete documents of a collection.One is db.collection.deleteOne() _and another one is _db.collection.deleteMany().

  • deleteOne()

It is used to delete a single document from the collection.

Example

db.cars.deleteOne(
//deletes one laptop having model "2013"
{ "model": "2013" }
)

  • deleteMany()

It is used to delete multiple documents from the collection.

Example

db.cars.deleteMany(
//delete all cars having model "2013"
{ "model": "2013" }
)

Top comments (0)