update
The `update` command is used to modify existing documents within a collection. The `update` command can be used to update one or multiple documents based on filtering criteria. Values of fields can be changed, new fields and values can be added and existing fields can be removed.
Modifies documents in a collection using various update operators like $set, $inc, $min, $max, and more.
Syntax
db.collection.updateOne(
<filter>,
<update>,
<options>
);
db.collection.updateMany(
<filter>,
<update>,
<options>
);
Parameters
filter
objectrequiredA document that specifies the criteria for selecting documents to update.
update
objectrequiredA document that specifies the modifications to apply using update operators.
options
objectA document that specifies options for the update operation, including `upsert` and `writeConcern`.
Examples
Sample Data
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Month": 6
}
}
}
]
}
Update a single document using the $inc operator
Increment the totalSales by 10 and decrement the number of full time staff for a document with the specified _id.
The `$inc` operator increments or decrements field values by a specified amount.
Query:
db.stores.updateOne(
{"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4"},
{"$inc": {"sales.salesByCategory.0.totalSales": 10, "staff.totalStaff.fullTime": -6}}
)
Output:
{
acknowledged: true,
matchedCount: 1,
modifiedCount: 1
}
Update a single document using the $min operator
Update the totalStaff count for the document with the specified _id to 10 if the current value of the field is greater than 10.
The `$min` operator updates the field value only if the specified value is less than the current value.
Query:
db.stores.updateOne(
{"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4"},
{"$min": {"staff.totalStaff.fullTime": 10}}
)
Output:
{
acknowledged: true,
matchedCount: 1,
modifiedCount: 1
}
Update a single document using the $max operator
Update the totalStaff count for the document with the specified _id to 14 if the current value of the field is less than 14.
The `$max` operator updates the field value only if the specified value is greater than the current value.
Query:
db.stores.updateOne(
{"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4"},
{"$max": {"staff.totalStaff.fullTime": 14}}
)
Output:
{
acknowledged: true,
matchedCount: 1,
modifiedCount: 1
}
Update a single document using the $mul operator
Multiply the count of part time employees by 2 for the document with the specified _id value.
The `$mul` operator multiplies the field value by a specified number.
Query:
db.stores.updateOne(
{"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4"},
{"$mul": {"staff.totalStaff.partTime": 2}}
)
Output:
{
acknowledged: true,
matchedCount: 1,
modifiedCount: 1
}
Update a single document using the $rename operator
Rename the totalSales and totalStaff fields to fullSales and staffCounts respectively.
The `$rename` operator renames a field in a document.
Query:
db.stores.updateOne(
{"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4"},
{"$rename": {"sales.totalSales": "sales.fullSales", "staff.totalStaff": "staff.staffCounts"}}
)
Output:
{
acknowledged: true,
matchedCount: 1,
modifiedCount: 1
}
Update a single document using the $set operator
Set the fullSales field to 3700 for the document with the specified _id value.
The `$set` operator sets the value of a field in a document.
Query:
db.stores.updateOne(
{"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4"},
{"$set": {"sales.fullSales": 3700}}
)
Output:
{
acknowledged: true,
matchedCount: 1,
modifiedCount: 1
}
Update a single document using the $unset operator
Remove the lon field from the location object in the document with the specified _id value.
The `$unset` operator removes a field from a document.
Query:
db.stores.updateOne(
{"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4"},
{"$unset": {"location.lon": ""}}
)
Output:
{
acknowledged: true,
matchedCount: 1,
modifiedCount: 1
}
Update multiple documents
Update all documents where the first promotional event starts in February to start in March.
The `updateMany` command modifies all documents that match the filter criteria.
Query:
db.stores.updateMany(
{"promotionEvents.0.promotionalDates.startDate.Month": 2},
{"$inc": {"promotionEvents.0.promotionalDates.startDate.Month": 1}}
)
Output:
{
acknowledged: true,
matchedCount: 156,
modifiedCount: 156
}
Upsert a single document
Set the upsert flag to true to create a new document if the document specified in the query filter does not exist in the collection.
When `upsert` is true, a new document is created if no matching document is found.
Query:
db.stores.updateOne(
{"_id": "NonExistentDocId"},
{"$set": {"name": "Lakeshore Retail", "sales.totalSales": 0}},
{"upsert": true}
)
Output:
{
acknowledged: true,
matchedCount: 0,
modifiedCount: 0,
upsertedId: "NonExistentDocId"
}