CRUD Operation
Md Nazmul Islam
Posted on December 25, 2021
MongoDB is a document-oriented database program widely classified as a NoSQL database program.
In MongoDB, the CRUD operation refers to the creating, reading, updating, and deleting documents. Here is an explanation of the operations in detail:
Create
Create (or insert) operations add new documents to a collection. There are two ways to add new documents to a collection:
- db.collection.insertOne()
- db.collection.insertMany() insertOne() operation allows us to create individual documents in a collection, while the insertMany() operation is used to create multiple documents in a single operation.
Example 1:
Here is an example of how we can add a single car to the cars collection using the insertOne() operation:
db.cars.insertOne(
//inserting Bugatti Veyron Mansory Vivere-2005 into cars collection
{
name: "Bugatti Veyron Mansory Vivere"
model: "2005"
}
)
Example 2:
Now we will see how we can add info of multiple cars to the cars collection with a single operation using 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"
}])
Read
Read operations retrieve documents from a collection. Here is the method in Mongodb to retrieve information:
db.collection.find()
find() operation will return everything from a collection if you call it without any parameters. On the other hand, we can specify any filter or criteria to retrieve information from a collection using:db.collection.find(query)
Example:
Here is an example of how we can read information about all cars from the cars collection:
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" }
Update
Update operations modify existing documents in a collection. There are three ways to update documents of a collection:
db.collection.updateOne()
– Updates one field in the document where the given criteria or filter meets the condition. Updating a field will not remove the old field instead a new field will be added to the document.db.collection.updateMany()
– Updates all fields in the document where the given criteria or filter meets the condition.db.collection.replaceOne()
– Replace the entire document. It will replace the old fields and values with new ones.
Example: if we have the following document:
{
"_id" : ObjectId("1"),
"model" : 2005
}
Using:
replaceOne({"_id" : ObjectId("1")}, { "new_model" : 2020})
will result in:
{
"_id" : ObjectId("1"),
"new_model" : 2020
}
While using:
updateOne({"_id" : ObjectId("1")}, {$set: { "new_model" : 2020}})
Will result in:
{
"_id" : ObjectId("1"),
"model" : 2005,
"new_model" : 2020
}
While using:
updateMany({"_id" : ObjectId("1")}, {$set: { "name" : "NewName"}, $set: { "new_model" : 2020}})
will result in:
{
"_id" : ObjectId("1"),
"model" : 2005,
"new_model" : 2020
"name" : "newName"
}
Delete
Delete operations delete documents from a collection. There are two methods to delete documents of a collection:
- db.collection.deleteOne()
- db.collection.deleteMany()
deleteOne() method removes only the first document matched by the query filter document, and deleteMany() deletes multiple objects at once.
Example 1:
Here is an example of how we can remove only one car having the model “2013” from the cars collection:
db.cars.deleteOne( //deletes one car having model "2013" { "model": "2013" } )
Example 2: Here is an example of how all cars, having the model “2013” - can be deleted from the cars collection:
db.cars.deleteMany(
//delete all cars having model "2013"
{ "model": "2013" }
)
Posted on December 25, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 5, 2024