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 $[] operator in Azure Cosmos DB for MongoDB vCore is used to update all elements in an array that match a specified condition. This operator allows you to perform updates on multiple elements in an array without specifying their positions. It is particularly useful when you need to apply the same update to all items in an array.
Syntax
The syntax for using the $[] array update operator is as follows:
db.collection.update(
<query>,
{
$set: {
"<arrayField>.$[]": <value>
}
}
)
Parameters
Parameter | Description |
---|---|
<query> |
The selection criteria for the documents to update. |
<arrayField> |
The field containing the array to update. |
<value> |
The value to set for each matching element in the array. |
Example(s)
Example 1: Updating Discount Percentages
Suppose you want to update the discount percentage for all categories in the "Summer Sale" promotion event. You can use the $[] operator to achieve this:
db.stores.update(
{ "store.storeId": "12345", "store.promotionEvents.eventName": "Summer Sale" },
{
$set: {
"store.promotionEvents.$[event].discounts.$[].discountPercentage": 5
}
},
{
arrayFilters: [{ "event.eventName": "Summer Sale" }]
}
)
Example 2: Updating Sales by Category
If you want to increase the total sales for all categories by 10%, you can use the $[] operator as follows:
db.stores.update(
{ "store.storeId": "12345" },
{
$mul: {
"store.sales.salesByCategory.$[].totalSales": 1.10
}
}
)
Related content
- Review options for Migrating from MongoDB to Azure Cosmos DB for MongoDB (vCore)
- Read more about Feature compatibility with MongoDB