DEV Community

Feroj Alam
Feroj Alam

Posted on

Explanation on mongodb CRUD operations

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

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

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

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

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

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

We make update operations in these document.

updateOne({"_id" : ObjectId("1")}, {$set: { "new_model" : 2020}})
Output:
{
   "_id" : ObjectId("1"),
   "model" : 2005,
   "new_model" : 2020
}
Enter fullscreen mode Exit fullscreen mode
updateMany({"_id" : ObjectId("1")}, {$set: { "name" : "NewName"}, $set: { "new_model" : 2020}})
Output:
{
   "_id" : ObjectId("1"),
   "model" : 2005,
   "new_model" : 2020
   "name" : "newName"
}
Enter fullscreen mode Exit fullscreen mode
replaceOne({"_id" : ObjectId("1")}, { "new_model" : 2020})
Output:
{
   "_id" : ObjectId("1"),
   "new_model" : 2020
}
Enter fullscreen mode Exit fullscreen mode

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

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

It will delete only one car object of 2013 model.

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

It will delete all car objects of the 2013 model.

Oldest comments (0)