We saw projection in MongoDB in the last tutorial. Now we’ll look at the many actions that can be performed on MongoDB Documents, such as insert, query, update, and delete. We will be looking at each operation separately in this tutorial with their syntax and examples.
This tutorial on MongoDB documents will cover the syntax, SQL commands, and examples for inserting, deleting, updating, and querying MongoDB documents. So let’s get going.
Create / Insert
operation to create (or insert) add fresh paperwork to a collection. New documents may be added to a collection in one of two ways:
db.collection.insertOne()
db.collection.insertMany()
While the insertMany() action creates several documents in a single operation, the insertOne() operation allows us to add individual documents to a collection.
insertOne()
Here is an illustration of how to use the insertOne() action to add a single car to the cars collection:
db.cars.insertOne(
{
name: "Ferrari"
model: "2005"
}
)
insertMany()
Now we’ll see at how we can use insertMany() to add information about numerous cars to the cars collection in a single operation.
db.cars.insertMany([{
name: "Ferrai"
model: "2005"
},{
name: "Aston"
model: "2018"
},{
name: "Mercedes"
model: "2013"
}])
Read
Documents are retrieved from a collection through read operations. The way to retrieve data in MongoDB is as follows:
db.collection.find()
If you call the find() operation without any parameters, it will return every item from the collection. As opposed to this, we can use any filter or set of criteria to get data from a collection using:
db.collection.find(query)
Example 1
Here is an illustration of how to read information on every vehicle in the collection of vehicles:
db.cars.find()
{ "_id" : ObjectId("1"), "name" : "Ferrari ", "model" : "2005" }
{ "_id" : ObjectId("2"), "name" : "Aston", "model" : "2018" }
{ "_id" : ObjectId("3"), "name" : "Mercedes", "2013" : "2005" }
Example 2
We’ll now demonstrate how to read information about the 2005-model cars in the collection:
db.cars.find({"model": "2005"})
{ "_id" : ObjectId("1"), "name" : "Ferrari ", "model" : "2005" }
Update
Update operations change already-existing collection documents. A collection of papers can be updated in one of three ways:
db.collection.updateOne()
When the specified criteria or filter meets the condition, it updates one field in the document. When a field is updated, a new field is added to the document rather than the old field being deleted.
db.collection.updateMany()
Updates all document fields where the specified criteria or filter satisfies the requirement.
db.collection.replaceOne()
Replace the whole thing. New fields and values will be used in place of the existing ones.
Example
If we have the following document, for instance:
{
"_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
Documents in a collection are removed through deletion actions. A group of documents can be deleted using one of two methods:
db.collection.deleteOne()
db.collection.deleteMany()
While the deleteMany() function deletes numerous objects at once, the deleteOne() method just removes the first document that matches the query filter document.
Example 1
Here is an illustration of how we might eliminate just one 2013-model vehicle from the list of vehicles:
db.cars.deleteOne(
{ "model": "2013" }
)
Example 2
Here is an example of how to remove all vehicles with the model number “2013” from your collection of vehicles:
db.cars.deleteMany(
{ "model": "2013" }
)
Note:
- MongoDB’s insert, update, and delete operations each focus on a single collection.
- All write actions performed on a single document in MongoDB are atomic.