MongoDB: Update Embedded Documents
Learn how to update array fields in documents in MongoDB collections.
MongoDB provides the following methods to update existing documents in a collection:
To demonstrate the update operation, insert the following sample documents in the employees
collection.
db.employees.insertMany([
{
_id:1,
firstName: "John",
lastName: "King",
email: "[email protected]",
salary: 5000,
skills: [ "Angular", "React", "MongoDB" ]
},
{
_id:2,
firstName: "Sachin",
lastName: "T",
email: "[email protected]",
salary: 8000,
skills: [ "Accounting", "Tax" ]
},
{
_id:3,
firstName: "James",
lastName: "Bond",
email: "[email protected]",
salary: 7500,
skills: [ "Sales", "Marketing" ]
},
{
_id:4,
firstName: "Steve",
lastName: "J",
email: "[email protected]",
salary: 7000,
skills: [ "Mac", "Marketing", "Product Design" ]
},
{
_id:5,
firstName: "Kapil",
lastName: "D",
email: "[email protected]",
salary: 4500,
skills: [ "Accounting", "Tax", "Sales" ]
},
{
_id:6,
firstName: "Amitabh",
lastName: "B",
email: "[email protected]",
salary: 7000,
skills: [ "Marketing", "Tax" ]
}
])
Update a Single Field
The following updates a single field in a single document in employees
collection.
db.employees.updateOne({_id:1}, { $set: {firstName:'Morgan'}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
In the above example, the first parameter is the filter criteria specified as a document, {_id:1}
indicates that find a document whose _id
is 1. The second parameter is used to specify fields and values to be modified on the matching document in the {<update-operator>: { field: value, field:value,... }
format. Use the update operator to specify what action to perform. Here we want to set the value of a field, so use $set
operator to specify fields and updated values in {field:updated-value}
format.{ $set: {firstName:'Morgan'}}
modifies the firstName
to "Morgan"
to the first document that matches with the specified criteria {_id:1}
.
In the output, matchedCount
indicates the number of documents that matched with the criteria, and modifiedCount
indicates the number of documents updated. The updateOne()
method will always modify a single document.
Now, check whether it has updated a value or not using the findOne()
method shown below.
db.employees.find({_id:1})
{
_id: 1,
firstName: 'Morgan',
lastName: 'King',
email: '[email protected]',
salary: 5000,
skills: [ 'Angular', 'React', 'MongoDB' ],
department: { name: 'IT' }
}
The updateOne()
method adds the specified field if it does not exist in a matching document. For example, the following will add the location
field.
db.employees.updateOne({firstName:"Steve"}, { $set: {location: "USA"}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
Execute the following find()
method to see the updated data.
db.employees.find({firstName:"Steve"})
{
_id:4,
firstName: "Steve",
lastName: "J",
email: "[email protected]",
salary: 7000,
location:"USA"
}
Use the $inc
update operator to increase the value of the field by the specified amount.
db.employees.updateOne({firstName:"Steve"}, { $inc: {salary: 500}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
Execute the following find()
method to see the updated data.
db.employees.find({firstName:"Steve"})
{
_id:4,
firstName: "Steve",
lastName: "J",
email: "[email protected]",
salary: 7500,
location:"USA"
}
Update Multiple Fields
You can also specify multiple fields to update. The following updates email
and lastName
fields.
db.employees.updateOne({_id:2}, { $set: {lastName:"Tendulkar", email:"[email protected]"}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
Execute the following find()
method to see the updated data.
db.employees.find({_id:2})
{
_id:2,
firstName: "Sachin",
lastName: "Tendulkar",
email: "[email protected]",
salary: 8000,
skills: [ "Accounting", "Tax" ],
department: {
"name":"Finance"
}
}
The updateOne()
method updates a single document only, even if it finds multiple documents. For example, the following updates the first document even if it returns multiple documents.
db.employees.updateOne({salary:7000}, { $set: {salary:7500}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
In the above example, the employees
collection contains two documents that have salary:7000
field. However, the updateOne()
modified a single document which is the first document from the matching result.
Update Array Elements
The $set
operator overwrites the specified array instead of adding, removing, and updating array elements.
db.employees.updateOne({_id:5},{$set:{ skills:["Sales Tax"]}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
In the above example, {$set:{ skills:["Sales Tax"]}}
overwrites an existing array for {_id:5}
.
db.employees.findOne({_id:5})
{
_id: 5,
firstName: 'Kapil',
lastName: 'D',
email: '[email protected]',
salary: 4500,
skills: [ 'Sales Tax' ],
department: { name: 'Finance' },
location: 'USA'
}
Use the array operators to update single or multiple elements. The following updates an array element if found the specified element.
db.employees.updateOne({_id:2}, { $set: {"skills.$[element]":"Sales Tax"}},{ arrayFilters: [{ element: "Tax" }]})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
In the above example, {$set:{"skills.$[element]":"Sales Tax"}},{ arrayFilters: [{ element: "Tax" }]
updates the skills
array if it contains "Tax" element then updates it to "Sales Tax".{"skills.$[element]":"Sales Tax"}
specifies that the make the array element value to "Sales Tax" where array filter specifies the criteria { arrayFilters: [{ element: "Tax" }]
where array element is "Tax". So, it will update array element if found matching element specified by arrayFilters
.