CRUD OPERATION IS EASY PEASY IF YOU READ THIS!
Published: 2021-12-23 17:06:52

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