Skip to content

Commit ed722db

Browse files
authored
Revise $objectToArray documentation details
1 parent 8ff51c6 commit ed722db

File tree

1 file changed

+126
-10
lines changed

1 file changed

+126
-10
lines changed

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)]

0 commit comments

Comments
 (0)