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 (or insert) operations add new documents to a collection. There are two ways to add new documents to a collection:
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.
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"
}
)
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 operations retrieve documents from a collection. Here is the method in Mongodb to retrieve information:
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:
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" }
Now we will see how we can read information about those cars from the cars
collection whose model is 2005:
db.cars.find({"model": "2005"}) // with one paramter
Output:
{ "_id" : ObjectId("1"), "name" : "Bugatti Veyron Mansory Vivere", "model" : "2005" }
Update operations modify existing documents in a collection. There are three ways to update documents of a collection:
– 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.
– Updates all fields in the document where the given criteria or filter meets the condition.
– Replace the entire document. It will replace the old fields and values with new ones.
For 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 operations delete documents from a collection. There are two methods to delete documents of a collection::
deleteOne()
method removes only the first document matched by the query filter document, and deleteMany()
deletes multiple objects at once.
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" }
)
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" }
)
Note:
- Insert, update, and delete actions in MongoDB all target a single collection.
- On the level of a single document, all write operations in MongoDB are atomic.
Free Resources