-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
objectToArray.txt
178 lines (129 loc) · 4.67 KB
/
objectToArray.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
============================
$objectToArray (aggregation)
============================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. expression:: $objectToArray
.. versionadded:: 3.4.4
Converts a document to an array. The return array contains an
element for each field/value pair in the original document.
Each element in the return array is a document that contains two
fields ``k`` and ``v``:
- The ``k`` field contains the field name in the original document.
- The ``v`` field contains the value of the field in the original
document.
:expression:`$objectToArray` has the following syntax:
.. code-block:: javascript
{ $objectToArray: <object> }
The ``<object>`` expression can be any valid :ref:`expression
<aggregation-expressions>` as long as it resolves to a document
object. :expression:`$objectToArray` applies to the top-level fields
of its argument. If the argument is a document that itself contains
embedded document fields, the :expression:`$objectToArray` does not
recursively apply to the embedded document fields.
For more information on expressions, see
:ref:`aggregation-expressions`.
Behavior
--------
For more information on expressions, see :ref:`aggregation-expressions`.
.. list-table::
:header-rows: 1
:widths: 90 10
* - Example
- Results
* - ``{ $objectToArray: { item: "foo", qty: 25 } }``
- .. code-block:: json
[
{
"k" : "item",
"v" : "foo"
},
{
"k" : "qty",
"v" : 25
}
]
* - .. code-block:: json
{ $objectToArray: {
item: "foo",
qty: 25,
size: { len: 25, w: 10, uom: "cm" }
} }
- .. code-block:: json
[
{
"k" : "item",
"v" : "foo"
},
{
"k" : "qty",
"v" : 25
},
{
"k" : "size",
"v" : {
"len" : 25,
"w" : 10,
"uom" : "cm"
}
}
]
Examples
--------
``$objectToArray`` Example
~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider a ``inventory`` collection with the following documents:
.. code-block:: javascript
{ "_id" : 1, "item" : "ABC1", dimensions: { l: 25, w: 10, uom: "cm" } }
{ "_id" : 2, "item" : "ABC2", dimensions: { l: 50, w: 25, uom: "cm" } }
{ "_id" : 3, "item" : "XYZ1", dimensions: { l: 70, w: 75, uom: "cm" } }
The following aggregation pipeline operation use the
:expression:`$objectToArray` to return the ``dimensions`` field as an array:
.. cssclass:: copyable-code
.. code-block:: javascript
db.inventory.aggregate(
[
{
$project: {
item: 1,
dimensions: { $objectToArray: "$dimensions" }
}
}
]
)
The operation returns the following:
.. code-block:: javascript
{ "_id" : 1, "item" : "ABC1", "dimensions" : [ { "k" : "l", "v" : 25 }, { "k" : "w", "v" : 10 }, { "k" : "uom", "v" : "cm" } ] }
{ "_id" : 2, "item" : "ABC2", "dimensions" : [ { "k" : "l", "v" : 50 }, { "k" : "w", "v" : 25 }, { "k" : "uom", "v" : "cm" } ] }
{ "_id" : 3, "item" : "XYZ1", "dimensions" : [ { "k" : "l", "v" : 70 }, { "k" : "w", "v" : 75 }, { "k" : "uom", "v" : "cm" } ] }
``$objectToArray`` to Sum Nested Fields
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider a ``inventory`` collection with the following documents:
.. code-block:: javascript
{ "_id" : 1, "item" : "ABC1", instock: { warehouse1: 2500, warehouse2: 500 } }
{ "_id" : 2, "item" : "ABC2", instock: { warehouse2: 500, warehouse3: 200} }
The following aggregation pipeline operation uses the
:expression:`$objectToArray` along with :pipeline:`$unwind` and
:pipeline:`$group` to calculate the total items in stock per warehouse.
.. cssclass:: copyable-code
.. code-block:: javascript
db.inventory.aggregate([
{ $project: { warehouses: { $objectToArray: "$instock" } } },
{ $unwind: "$warehouses" },
{ $group: { _id: "$warehouses.k", total: { $sum: "$warehouses.v" } } }
])
The operation returns the following:
.. code-block:: javascript
{ "_id" : "warehouse3", "total" : 200 }
{ "_id" : "warehouse2", "total" : 1000 }
{ "_id" : "warehouse1", "total" : 2500 }
``$objectToArray`` + ``$arrayToObject`` Example
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. include:: /includes/example-objectToArray-arrayToObject.rst
.. seealso:: :expression:`$arrayToObject`