MongoDB Update Documents
MongoDB Update Documents
MongoDB Update Documents
Syntax:
db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA)
Example
Step 2) Choose the condition which you want to use to decide which document
needs to be updated. In our example, we want the document which has the
Employee id of "1" to be updated.
Step 3) Choose which Field Name's you want to modify and enter their new value
accordingly.
db.Employee.update
(
{
Employeeid : 1
},
{
$set :
{
"EmployeeName" : "NewMartin",
"Employeeid" : 22
}
}
)
db.people.update(
{
"name":"john"
},
{
$set:
{
"age":80
}
}
)
db.people.updateOne(
{
"name":"john"
},
{
$set:{"age":80}
}
db.people.updateMany(
{
"name":"john"
},
{
$set:{"age":80}
}
)
Update john's marks to 55 where marks are 50 (Use the positional operator $):
db.students.update(
{
"name": "john",
"marks": 50
},
{
"$set": {"marks.$": 55}
})
Update Values in an Array
▪ The positional $ operator identifies an element in an array to update without
explicitly specifying the position of the element in the array.
1. Deletion criteria: With the use of its syntax you can remove the documents from the
collection.
1. Deletion criteria: With the use of its syntax you can remove the documents from the
collection.
Syntax:
1. db.collection_name.remove (DELETION_CRITERIA)
MongoDB's remove() method. If you execute this command without any argument
or without empty argument it will remove all documents from the collection.
db.people.remove();
or
db.people.remove({});
The following example will remove all documents from the people collection where the name
field is equal to john.
db.people.remove({"name":"john"})
db.people.remove({"name":"john"},1)
> db.people.find()
> db.people.insertMany([ {name:"Any", age:"21", status:"busy"},
{name:"Tony", age:"25", status:"busy"}, {name:"Bobby", age:"28",
status:"online"}, {name:"Sonny", age:"28", status:"away"}, {name:"Cher",
age:"20", status:"online"} ])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5f22c6cec6f38e78ac0cbd5a"),
ObjectId("5f22c6cec6f38e78ac0cbd5b"),
ObjectId("5f22c6cec6f38e78ac0cbd5c"),
ObjectId("5f22c6cec6f38e78ac0cbd5d"),
ObjectId("5f22c6cec6f38e78ac0cbd5e")
]
}
> db.people.find()
{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5a"), "name" : "Any", "age" : "21",
"status" : "busy" }
{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5b"), "name" : "Tony", "age" : "25",
"status" : "busy" }
{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5c"), "name" : "Bobby", "age" : "28",
"status" : "online" }
{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5d"), "name" : "Sonny", "age" : "28",
"status" : "away" }
{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5e"), "name" : "Cher", "age" : "20",
"status" : "online" }
db.collection.find({});
> db.people.find().skip(3)
> db.people.find().count()
5
To sort descending by the field name:
> db.people.find().sort({"name":-1})
{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5b"), "name" : "Tony", "age" : "25",
"status" : "busy" }
To sort ascending by the field name: If you want to sort ascending just replace
-1 with 1
> db.people.find().sort({"name":1})
db.collection_name.limit(n)
> db.people.find().limit(3)
> db.people.find().skip(1).limit(2)
Also combinations of this methods are allowed. For example get 2 documents
from descending sorted collection skipping the first 1:
db.collection_name.find({"field_name":{$gt:criteria_value}}).pretty()
For example: I would like to fetch the details of person having age > 25 then
the query should be:
> db.people.find({"age":{$gt:"25"}})
db.collection_name.find({"field_name":{$lt:criteria_value}}).pretty()
For example: I would like to fetch the details of person having age < 25 then
the query should be:
> db.people.find({"age":{$lt:"25"}})
db.collection_name.find({"field_name":{$ne:criteria_value}}).pretty()
Example: Find all the people where age is not equal to 20. The command
for this criterion would be:
> db.people.find({"age":{$ne:"20"}})
{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5a"), "name" : "Any", "age" : "21", "status" : "busy" }