Explanation on mongodb CRUD operations
Published: 2021-12-30 12:24:17

Mongodb is used to store data that is a NoSQL database. Mongodb is a document-oriented database. In database we make CRUD operations to store and use data. C means Create, R means Read, U means update, and D means delete data. Now we know about these in detail.

**Create:** Create operations are used to insert documents into a collection.We can use it two ways. They are-

```
db.collection.insertOne()
db.collection.insertMany()
```
Using insertOne() we can create single document in a collection and using insertMany() we can create more than one document in a collection.
Example: Use of insertOne()

```
db.cars.insertOne(
//inserting Bugatti Veyron Mansory Vivere-2005 into cars collection
    {
       name: "Bugatti Veyron Mansory Vivere"
       model: "2005"
    }
)
```
Here one car data is inserted into car collection using insertOne() method.
Use of insertMany()

```
db.cars.insertMany([{
//inserting three cars along with their models into cars collection
       name: "Bugatti Veyron Mansory Vivere"
       model: "2005"
},{
      name: "Aston Martin AM-RB 001"
       model: "2018"
},{
       name: "Ferrari Pininfarina Sergio"
       model: "2013"
}])
```
Here three car data is inserted into car collection using insertMany() method.
**Read:** Read operation is used to get data from a collection. We can use two method to get data. They are- 

```
db.collection.find()
db.collection.find(query)
```
Using find() method we can get all document from a collection because this method returns all document if we do not give any parameter. If we give any parameter to filter or specify document then it returns the filtered documents.
Example:

```
 db.cars.find() // no parameters
Output:
{ "_id" : ObjectId("1"), "name" : "Bugatti Veyron Mansory Vivere", "model" : "2005" }
{ "_id" : ObjectId("2"), "name" : "Aston Martin AM-RB 001", "model" : "2018" }
{ "_id" : ObjectId("3"), "name" : "Ferrari Pininfarina Sergio", "2013" : "2005" }
```
 
 

```
db.cars.find({"model": "2005"}) // with one paramter
output:
{ "_id" : ObjectId("1"), "name" : "Bugatti Veyron Mansory Vivere", "model" : "2005" }
Update: Update operation is used to modify document into a collection. We can use three methods to modify the existing document. They are- 
db.collection.updateOne()
db.collection.updateMany()
db.collection.replaceOne()
```
When we use updateOne() method it modify the given filtered document. It does not remove the field rather than it will add a new field to the document.
When we use updateMany() method it will modify all the documents which are given for filter.
When we use replaceOne() method it replace the entire document. It will replace the old fields and values with new data.
Example: 

```
{
   "_id" : ObjectId("1"),
   "model" : 2005
}
```
We make update operations in these document.

```
updateOne({"_id" : ObjectId("1")}, {$set: { "new_model" : 2020}})
Output:
{
   "_id" : ObjectId("1"),
   "model" : 2005,
   "new_model" : 2020
}
```

```
updateMany({"_id" : ObjectId("1")}, {$set: { "name" : "NewName"}, $set: { "new_model" : 2020}})
Output:
{
   "_id" : ObjectId("1"),
   "model" : 2005,
   "new_model" : 2020
   "name" : "newName"
}
```

```
replaceOne({"_id" : ObjectId("1")}, { "new_model" : 2020})
Output:
{
   "_id" : ObjectId("1"),
   "new_model" : 2020
}
```
**Delete: **Delete operation is used to remove documents from a collection. We can use two methods to delete documents. They are- 

```
db.collection.deleteOne()
db.collection.deleteMany()
```
When we use deleteOne() method it removes only the first document which is matched by query filter.
When we use deleteMany() method it will remove multiple documents at a time.
Example:

```
 db.cars.deleteOne(
//deletes one car having model "2013"
    { "model": "2013" }
)
```
It will delete only one car object of 2013 model.

```
db.cars.deleteMany(
//delete all cars having model "2013"
    { "model": "2013" }
)
```
It will delete all car objects of the 2013 model.