Skip to content

Commit 9b4a06e

Browse files
Merge pull request #2987 from abinav2307/object-expression-third-pass-validation
Object expression third pass validation
2 parents b1988c1 + 4fe84ec commit 9b4a06e

File tree

3 files changed

+246
-18
lines changed

3 files changed

+246
-18
lines changed

articles/cosmos-db/mongodb/vcore/operators/object-expression/$mergeobjects.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
title: $mergeObjects
3-
titleSuffix: Overview of the $mergeObjects expression in Azure Cosmos DB for MongoDB (vCore)
3+
titleSuffix: Overview of the $mergeObjects operator in Azure Cosmos DB for MongoDB (vCore)
44
description: The $mergeObjects operator merges multiple documents into a single document
55
author: abinav2307
66
ms.author: abramees
77
ms.service: azure-cosmos-db
88
ms.subservice: mongodb-vcore
99
ms.topic: language-reference
10-
ms.date: 08/03/2025
10+
ms.date: 09/04/2025
1111
---
1212

1313
# $mergeObjects
@@ -30,7 +30,7 @@ The `$mergeObjects` operator combines multiple documents into a single document.
3030

3131
## Examples
3232

33-
Let's understand the usage with sample json from `stores` dataset.
33+
Consider this sample document from the stores collection.
3434

3535
```json
3636
{
@@ -144,7 +144,7 @@ Let's understand the usage with sample json from `stores` dataset.
144144

145145
### Example 1 - Merging documents as an accumulator to group documents by the sales subdocument
146146

147-
The query is an aggregation pipeline that uses $mergeObjects to merge all sales subdocuments per city for a specific company.
147+
The query merges all sales subdocuments per city for a specific company.
148148

149149
```javascript
150150
db.stores.aggregate([
@@ -167,7 +167,7 @@ db.stores.aggregate([
167167
])
168168
```
169169

170-
The query groups store documents by city for the company "Fourth Coffee" and merges their sales fields into a single object per city.
170+
The first two results returned by this query are:
171171

172172
```json
173173
[

articles/cosmos-db/mongodb/vcore/operators/object-expression/$objectToArray.md

Lines changed: 126 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
title: $objectToArray
3-
titleSuffix: Overview of the $objectToArray expression in Azure Cosmos DB for MongoDB (vCore)
3+
titleSuffix: Overview of the $objectToArray operator in Azure Cosmos DB for MongoDB (vCore)
44
description: The objectToArray command is used to transform a document (object) into an array of key-value pairs.
55
author: avijitgupta
66
ms.author: avijitgupta
77
ms.service: azure-cosmos-db
88
ms.subservice: mongodb-vcore
99
ms.topic: language-reference
10-
ms.date: 08/03/2025
10+
ms.date: 09/04/2025
1111
---
1212

1313
# $objectToArray
@@ -17,7 +17,9 @@ The `$objectToArray` operator is used to transform a document (object) into an a
1717
## Syntax
1818

1919
```javascript
20-
{ $objectToArray: <object> }
20+
{
21+
$objectToArray: <object>
22+
}
2123
```
2224

2325
## Parameters
@@ -28,11 +30,121 @@ The `$objectToArray` operator is used to transform a document (object) into an a
2830

2931
## Examples
3032

31-
Given a sample JSON document representing a store, we can use the `$objectToArray` operator to transform various parts of this document.
33+
Consider this sample document from the stores collection.
34+
35+
```json
36+
{
37+
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
38+
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
39+
"location": {
40+
"lat": -89.2384,
41+
"lon": -46.4012
42+
},
43+
"staff": {
44+
"totalStaff": {
45+
"fullTime": 8,
46+
"partTime": 20
47+
}
48+
},
49+
"sales": {
50+
"totalSales": 75670,
51+
"salesByCategory": [
52+
{
53+
"categoryName": "Wine Accessories",
54+
"totalSales": 34440
55+
},
56+
{
57+
"categoryName": "Bitters",
58+
"totalSales": 39496
59+
},
60+
{
61+
"categoryName": "Rum",
62+
"totalSales": 1734
63+
}
64+
]
65+
},
66+
"promotionEvents": [
67+
{
68+
"eventName": "Unbeatable Bargain Bash",
69+
"promotionalDates": {
70+
"startDate": {
71+
"Year": 2024,
72+
"Month": 6,
73+
"Day": 23
74+
},
75+
"endDate": {
76+
"Year": 2024,
77+
"Month": 7,
78+
"Day": 2
79+
}
80+
},
81+
"discounts": [
82+
{
83+
"categoryName": "Whiskey",
84+
"discountPercentage": 7
85+
},
86+
{
87+
"categoryName": "Bitters",
88+
"discountPercentage": 15
89+
},
90+
{
91+
"categoryName": "Brandy",
92+
"discountPercentage": 8
93+
},
94+
{
95+
"categoryName": "Sports Drinks",
96+
"discountPercentage": 22
97+
},
98+
{
99+
"categoryName": "Vodka",
100+
"discountPercentage": 19
101+
}
102+
]
103+
},
104+
{
105+
"eventName": "Steal of a Deal Days",
106+
"promotionalDates": {
107+
"startDate": {
108+
"Year": 2024,
109+
"Month": 9,
110+
"Day": 21
111+
},
112+
"endDate": {
113+
"Year": 2024,
114+
"Month": 9,
115+
"Day": 29
116+
}
117+
},
118+
"discounts": [
119+
{
120+
"categoryName": "Organic Wine",
121+
"discountPercentage": 19
122+
},
123+
{
124+
"categoryName": "White Wine",
125+
"discountPercentage": 20
126+
},
127+
{
128+
"categoryName": "Sparkling Wine",
129+
"discountPercentage": 19
130+
},
131+
{
132+
"categoryName": "Whiskey",
133+
"discountPercentage": 17
134+
},
135+
{
136+
"categoryName": "Vodka",
137+
"discountPercentage": 23
138+
}
139+
]
140+
}
141+
]
142+
}
143+
```
32144

33145
### Example 1: Transforming the `location` object
34146

35-
The example aggregation pipeline transforms the `location` object into an array of key-value pairs.
147+
This query transforms the `location` object into an array of key-value pairs.
36148

37149
```javascript
38150
db.stores.aggregate([
@@ -47,9 +159,10 @@ db.stores.aggregate([
47159
])
48160
```
49161

50-
The output represents the result of using the `$objectToArray` aggregation operator on the `location` field for each document.
162+
The first two results returned by this query are:
51163

52164
```json
165+
[
53166
{
54167
"_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
55168
"locationArray": [
@@ -76,11 +189,12 @@ The output represents the result of using the `$objectToArray` aggregation opera
76189
}
77190
]
78191
}
192+
]
79193
```
80194

81195
### Example 2: Transforming the `salesByCategory` array
82196

83-
If you want to transform the `salesByCategory` array, you would first need to unwind the array and then apply the `$objectToArray` operator.
197+
To transform the `salesByCategory` array, first unwind the array and then apply the `$objectToArray` operator.
84198

85199
```javascript
86200
db.stores.aggregate([
@@ -96,9 +210,7 @@ db.stores.aggregate([
96210
])
97211
```
98212

99-
Converting subdocuments to key-value pairs is often used when you want to dynamically process field names, especially when:
100-
- Building generic pipelines.
101-
- Mapping field names into key-value structures for flexible transformations or further processing.
213+
The first two results returned by this query are:
102214

103215
```json
104216
[
@@ -131,6 +243,10 @@ Converting subdocuments to key-value pairs is often used when you want to dynami
131243
]
132244
```
133245

246+
Converting subdocuments to key-value pairs is often used when you want to dynamically process field names, especially when:
247+
- Building generic pipelines.
248+
- Mapping field names into key-value structures for flexible transformations or further processing.
249+
134250
## Related content
135251

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

articles/cosmos-db/mongodb/vcore/operators/object-expression/$setField.md

Lines changed: 115 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
title: $setField
3-
titleSuffix: Overview of the $setField expression in Azure Cosmos DB for MongoDB (vCore)
3+
titleSuffix: Overview of the $setField operator in Azure Cosmos DB for MongoDB (vCore)
44
description: The setField command is used to add, update, or remove fields in embedded documents.
55
author: avijitgupta
66
ms.author: avijitgupta
77
ms.service: azure-cosmos-db
88
ms.subservice: mongodb-vcore
99
ms.topic: language-reference
10-
ms.date: 08/03/2025
10+
ms.date: 09/04/2025
1111
---
1212

1313
# $setField
@@ -36,6 +36,118 @@ The `$setField` operator is used to add, update, or remove fields in embedded do
3636

3737
## Examples
3838

39+
Consider this sample document from the stores collection.
40+
41+
```json
42+
{
43+
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
44+
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
45+
"location": {
46+
"lat": -89.2384,
47+
"lon": -46.4012
48+
},
49+
"staff": {
50+
"totalStaff": {
51+
"fullTime": 8,
52+
"partTime": 20
53+
}
54+
},
55+
"sales": {
56+
"totalSales": 75670,
57+
"salesByCategory": [
58+
{
59+
"categoryName": "Wine Accessories",
60+
"totalSales": 34440
61+
},
62+
{
63+
"categoryName": "Bitters",
64+
"totalSales": 39496
65+
},
66+
{
67+
"categoryName": "Rum",
68+
"totalSales": 1734
69+
}
70+
]
71+
},
72+
"promotionEvents": [
73+
{
74+
"eventName": "Unbeatable Bargain Bash",
75+
"promotionalDates": {
76+
"startDate": {
77+
"Year": 2024,
78+
"Month": 6,
79+
"Day": 23
80+
},
81+
"endDate": {
82+
"Year": 2024,
83+
"Month": 7,
84+
"Day": 2
85+
}
86+
},
87+
"discounts": [
88+
{
89+
"categoryName": "Whiskey",
90+
"discountPercentage": 7
91+
},
92+
{
93+
"categoryName": "Bitters",
94+
"discountPercentage": 15
95+
},
96+
{
97+
"categoryName": "Brandy",
98+
"discountPercentage": 8
99+
},
100+
{
101+
"categoryName": "Sports Drinks",
102+
"discountPercentage": 22
103+
},
104+
{
105+
"categoryName": "Vodka",
106+
"discountPercentage": 19
107+
}
108+
]
109+
},
110+
{
111+
"eventName": "Steal of a Deal Days",
112+
"promotionalDates": {
113+
"startDate": {
114+
"Year": 2024,
115+
"Month": 9,
116+
"Day": 21
117+
},
118+
"endDate": {
119+
"Year": 2024,
120+
"Month": 9,
121+
"Day": 29
122+
}
123+
},
124+
"discounts": [
125+
{
126+
"categoryName": "Organic Wine",
127+
"discountPercentage": 19
128+
},
129+
{
130+
"categoryName": "White Wine",
131+
"discountPercentage": 20
132+
},
133+
{
134+
"categoryName": "Sparkling Wine",
135+
"discountPercentage": 19
136+
},
137+
{
138+
"categoryName": "Whiskey",
139+
"discountPercentage": 17
140+
},
141+
{
142+
"categoryName": "Vodka",
143+
"discountPercentage": 23
144+
}
145+
]
146+
}
147+
]
148+
}
149+
```
150+
39151
### Example 1: Updating a nested field
40152

41153
This query performs a conditional update on nested discount values inside promotion events for the document matching a specific `_id`.
@@ -82,7 +194,7 @@ db.stores.updateOne(
82194

83195
### Example 2: Removing a field
84196

85-
Suppose you want to remove the `totalStaff` field from the `staff` object.
197+
This query removes the `totalStaff` field from the `staff` object.
86198

87199
```javascript
88200
db.collection.updateOne(

0 commit comments

Comments
 (0)