Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The $[identifier] array update operator in Azure Cosmos DB for MongoDB vCore is used to update specific elements in an array that match a given condition. This operator is particularly useful when you need to update multiple elements within an array based on certain criteria. It allows for more granular updates within documents, making it a powerful tool for managing complex data structures.
{
"<update operator>": {
"<array field>.$[<identifier>]": <value>
}
},
{
"arrayFilters": [
{ "<identifier>.<field>": <condition> }
]
}
Parameter | Description |
---|---|
<update operator> |
The update operator to be applied (e.g., $set , $inc , etc.). |
<array field> |
The field containing the array to be updated. |
<identifier> |
A placeholder used in arrayFilters to match specific elements in the array. |
<value> |
The value to be set or updated. |
arrayFilters |
An array of filter conditions to identify which elements to update. |
<field> |
The specific field within array elements to be checked. |
<condition> |
The condition that array elements must meet to be updated. |
Suppose you want to update the discount percentage for the "Laptops" category in the "Holiday Specials" promotion event.
db.collection.update(
{ "store.storeId": "12345", "store.promotionEvents.eventName": "Holiday Specials" },
{
$set: {
"store.promotionEvents.$[event].discounts.$[discount].discountPercentage": 18
}
},
{
arrayFilters: [
{ "event.eventName": "Holiday Specials" },
{ "discount.categoryName": "Laptops" }
]
}
)
Suppose you want to increase the total sales for the "Smartphones" category by $10,000.
db.collection.update(
{ "store.storeId": "12345" },
{
$inc: {
"store.sales.salesByCategory.$[category].totalSales": 10000
}
},
{
arrayFilters: [
{ "category.categoryName": "Smartphones" }
]
}
)
- Review options for Migrating from MongoDB to Azure Cosmos DB for MongoDB (vCore)
- Read more about Feature compatibility with MongoDB