Skip to content

Commit 863441a

Browse files
Merge pull request #2989 from abinav2307/projection-third-pass-validation
Projection third pass validation
2 parents 9b4a06e + b2ca05a commit 863441a

File tree

8 files changed

+190
-511
lines changed

8 files changed

+190
-511
lines changed

articles/cosmos-db/.openpublishing.redirection.cosmos-db.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,21 @@
675675
"redirect_url": "/azure/cosmos-db/mongodb/vcore/operators/aggregation/$unset",
676676
"redirect_document_id": true
677677
},
678+
{
679+
"source_path_from_root": "/articles/cosmos-db/mongodb/vcore/operators/projection/$slice.md",
680+
"redirect_url": "/azure/cosmos-db/mongodb/vcore/operators/array-expression/$slice",
681+
"redirect_document_id": false
682+
},
683+
{
684+
"source_path_from_root": "/articles/cosmos-db/mongodb/vcore/operators/projection/$elemmatch.md",
685+
"redirect_url": "/azure/cosmos-db/mongodb/vcore/operators/array-query/$elemmatch",
686+
"redirect_document_id": true
687+
},
688+
{
689+
"source_path_from_root": "/articles/cosmos-db/mongodb/vcore/operators/projection/$.md",
690+
"redirect_url": "/azure/cosmos-db/mongodb/vcore/operators/array-update/$",
691+
"redirect_document_id": true
692+
},
678693
{
679694
"source_path_from_root": "/articles/cosmos-db/nosql/troubleshoot-dot-net-sdk-request-timeout.md",
680695
"redirect_url": "/azure/cosmos-db/nosql/troubleshoot-dotnet-sdk-request-timeout",

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

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,116 @@ The query returns the following result.
280280
]
281281
```
282282

283+
### Example 3: Fetch the first matching element from an array
284+
285+
This query retrieves the first document from "sales.salesByCategory" array.
286+
287+
```javascript
288+
db.stores.find({
289+
"name": "Lakeshore Retail"
290+
}, {
291+
"_id": 1,
292+
"name": 1,
293+
"sales.salesByCategory": {
294+
$slice: 1
295+
}
296+
} // restricts the fields to be returned
297+
)
298+
```
299+
300+
This query returns the following result.
301+
302+
```json
303+
[
304+
{
305+
"_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60",
306+
"name": "Lakeshore Retail",
307+
"sales": {
308+
"salesByCategory": [
309+
{
310+
"categoryName": "Towel Racks",
311+
"totalSales": 13237
312+
}
313+
]
314+
}
315+
}
316+
]
317+
```
318+
319+
### Example 4: Fetch the last element from an array
320+
321+
This query retrieves the last document from "sales.salesByCategory" array.
322+
323+
```javascript
324+
db.stores.find({
325+
"name": "Lakeshore Retail"
326+
}, {
327+
"_id": 1,
328+
"name": 1,
329+
"sales.salesByCategory": {
330+
$slice: -1
331+
}
332+
})
333+
```
334+
335+
This query returns the following result.
336+
337+
```json
338+
[
339+
{
340+
"_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60",
341+
"name": "Lakeshore Retail",
342+
"sales": {
343+
"salesByCategory": [
344+
{
345+
"categoryName": "Pillow Cases",
346+
"totalSales": 38833
347+
}
348+
]
349+
}
350+
}
351+
]
352+
```
353+
354+
### Example 5: Retrieve a range of elements from an array
355+
356+
This query retrieves a subset range from "sales.salesByCategory" array.
357+
358+
```javascript
359+
db.stores.find({
360+
"name": "Lakeshore Retail"
361+
}, {
362+
"_id": 1,
363+
"name": 1,
364+
"sales.salesByCategory": {
365+
$slice: [3, 2]
366+
}
367+
})
368+
```
369+
370+
This query returns the following result.
371+
372+
```json
373+
[
374+
{
375+
"_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60",
376+
"name": "Lakeshore Retail",
377+
"sales": {
378+
"salesByCategory": [
379+
{
380+
"categoryName": "Toothbrush Holders",
381+
"totalSales": 47912
382+
},
383+
{
384+
"categoryName": "Hybrid Mattresses",
385+
"totalSales": 48660
386+
}
387+
]
388+
}
389+
}
390+
]
391+
```
392+
283393
## Related content
284394

285395
[!INCLUDE[Related content](../includes/related-content.md)]

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

articles/cosmos-db/mongodb/vcore/operators/projection/$.md

Lines changed: 0 additions & 132 deletions
This file was deleted.

0 commit comments

Comments
 (0)