MongoDB Update Documents

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

MongoDB update documents

In MongoDB, update() method is used to update or modify the existing documents


of a collection.

Syntax:

db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA)

Example

Update the entire object:

db.people.update({name: 'Tom'}, {age: 29, name: 'Tom'})


Step 1) Issue the update command

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
}
}
)

just update a single field of a document. In this case age:

db.people.update(
{
"name":"john"
},
{
$set:
{
"age":80
}
}
)

You can also update multiple documents simultaneously by adding a third


parameter. This query will update all documents where the name equals Tom:
db.people.update(
{
"name":"john"
},
{
$set:{"age":80}
},
{
multi:true
}
)

//Will update only first matching document

db.people.updateOne(
{
"name":"john"
},
{
$set:{"age":80}
}

//Will update all matching documents

db.people.updateMany(
{
"name":"john"
},
{
$set:{"age":80}
}
)

For the following schema:


{name: 'john', age: 28, marks: [50, 60, 70]}

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.

▪ Consider a collection student with the following documents:

{ "_id" : 1, "grades" : [ 80, 85, 90 ] }


{ "_id" : 2, "grades" : [ 88, 90, 92 ] }
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }

▪ To update 80 to 82 in the grades array in the first document, use the


positional $ operator if you do not know the position of the element in the
array:
db.students.update(
{
_id: 1, grades: 80
},
{
$set:
{
"grades.$" : 82
}
}
)
For the following schema:
{name: 'Tom', age: 28, marks: [{subject: "English", marks: 90},{subject: "Maths",
marks: 100}, {subject: "Computes", marks: 20}]}
Update Tom's English marks to 85 :
db.people.update({name: "Tom", "marks.subject":
"English"},{"$set":{"marks.$.marks": 85}})
Explaining above example:
By using {name: "Tom", "marks.subject": "English"} you will get the position of
the object in the marks array, where subject is English. In "marks.$.marks", $ is
used to update in that position of the marks array

MongoDB Delete documents


In MongoDB, the db.colloction.remove() method is used to delete documents from a
collection. The remove() method works on two parameters.

1. Deletion criteria: With the use of its syntax you can remove the documents from the
collection.

2. JustOne: It removes only one document when set to true or 1.

MongoDB Delete documents


In MongoDB, the db.colloction.remove() method is used to delete documents from a
collection. The remove() method works on two parameters.

1. Deletion criteria: With the use of its syntax you can remove the documents from the
collection.

2. JustOne: It removes only one document when set to true or 1.

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({});

Remove all documents that match a condition


If you want to remove a document that match a specific condition, call the remove()
method with the <query> parameter.

The following example will remove all documents from the people collection where the name
field is equal to john.

db.people.remove({"name":"john"})

Remove a single document that match a condition


If you want to remove a single document that match a specific condition, call the remove()
method with justOne parameter set to true or 1.
The following example will remove a single document from the peoplw collection where the
name field is equal to john.

db.people.remove({"name":"john"},1)

Querying for Data

> 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" }

retrieve all documents in a collection

db.collection.find({});

MongoDB Skip() Method


▪ The skip() method is used for skipping the given number of documents in
the Query result.

To skip first 3 documents:

> db.people.find().skip(3)

{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5d"), "name" : "Sonny", "age" : "28",


"status" : "away" }

{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5e"), "name" : "Cher", "age" : "20",


"status" : "online" }

To count the results:

> 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" }

{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5d"), "name" : "Sonny", "age" : "28",


"status" : "away" }

{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5e"), "name" : "Cher", "age" : "20",


"status" : "online" }

{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5c"), "name" : "Bobby", "age" : "28",


"status" : "online" }

{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5a"), "name" : "Any", "age" : "21",


"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})

{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5a"), "name" : "Any", "age" : "21",


"status" : "busy" }

{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5c"), "name" : "Bobby", "age" : "28",


"status" : "online" }

{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5e"), "name" : "Cher", "age" : "20",


"status" : "online" }

{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5d"), "name" : "Sonny", "age" : "28",


"status" : "away" }

{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5b"), "name" : "Tony", "age" : "25",


"startf5t

▪ This method limits the number of documents returned in response to a


particular query.
Syntax:
db.collection_name.find().limit(number_of_documents)

db.collection_name.limit(n)

To display top 3 documents:

> db.people.find().limit(3)

{ "_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" }

get 2 documents from top but skipping the first 1:

> db.people.find().skip(1).limit(2)

{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5b"), "name" : "Tony", "age" : "25",


"status" : "busy" }
{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5c"), "name" : "Bobby", "age" : "28",
"status" : "online" }

Also combinations of this methods are allowed. For example get 2 documents
from descending sorted collection skipping the first 1:

> db.people.find({}).sort({ "name" : -1}).skip(1).limit(2)

{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5d"), "name" : "Sonny", "age" : "28",


"status" : "away" }
{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5e"), "name" : "Cher", "age" : "20",
"status" : "online" }

Greater Than Criteria:


Syntax:

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"}})

{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5c"), "name" : "Bobby", "age" : "28",


"status" : "online" }

{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5d"), "name" : "Sonny", "age" : "28",


"status" : "away" }

Less than Criteria:


Syntax:

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"}})

{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5a"), "name" : "Any", "age" : "21",


"status" : "busy" }

{ "_id" : ObjectId("5f22c6cec6f38e78ac0cbd5e"), "name" : "Cher", "age" : "20",


"status" : "online" }
Not Equals Criteria:
Syntax:

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" }

{ "_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" }

You might also like