Skip to content

Commit 1ef3591

Browse files
authored
Update examples in array-update documentation
1 parent be93cf9 commit 1ef3591

File tree

1 file changed

+52
-2
lines changed
  • articles/cosmos-db/mongodb/vcore/operators/array-update

1 file changed

+52
-2
lines changed

articles/cosmos-db/mongodb/vcore/operators/array-update/$.md

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,57 @@ Consider this sample document from the stores collection.
7272
}
7373
```
7474

75-
### Example 1: Update discount percentage for a specific category
75+
### Example 1: Project the first element of an array, matching the condition
76+
77+
This query returns the first element within the `salesByCategory` array, for `DJ` equipment with `totalSales` greater than 35000.
78+
79+
```javascript
80+
db.stores.find({
81+
"sales.salesByCategory": {
82+
$elemMatch: {
83+
"categoryName": {
84+
$regex: "^DJ"
85+
}
86+
}
87+
},
88+
"sales.salesByCategory.totalSales": {
89+
$gt: 35000
90+
}
91+
}, {
92+
"sales.salesByCategory.$": 1
93+
}).limit(2)
94+
```
95+
96+
The first two results returned by this query are:
97+
98+
```json
99+
[
100+
{
101+
"_id": "d3c9df51-41bd-4b4e-a26b-b038d9cf8b45",
102+
"sales": {
103+
"salesByCategory": [
104+
{
105+
"categoryName": "DJ Speakers",
106+
"totalSales": 36972
107+
}
108+
]
109+
}
110+
},
111+
{
112+
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
113+
"sales": {
114+
"salesByCategory": [
115+
{
116+
"categoryName": "DJ Headphones",
117+
"totalSales": 35911
118+
}
119+
]
120+
}
121+
}
122+
]
123+
```
124+
125+
### Example 2: Update discount percentage for a specific category
76126

77127
The example updates the discount percentage for "Desks" category in the first matching promotion event.
78128

@@ -91,7 +141,7 @@ db.stores.updateOne(
91141
)
92142
```
93143

94-
### Example 2: Update sales category total
144+
### Example 3: Update sales category total
95145

96146
The example updates the total sales for a specific category using the `$ (positional operator)`.
97147

0 commit comments

Comments
 (0)