Navigation

$objectToArray (aggregation)

$objectToArray

New in version 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.

$objectToArray has the following syntax:

{ $objectToArray: <object> }

The <object> expression can be any valid expression as long as it resolves to a document object. $objectToArray applies to the top-level fields of its argument. If the argument is a document that itself contains embedded document fields, the $objectToArray does not recursively apply to the embedded document fields.

For more information on expressions, see Expressions.

For more information on expressions, see Expressions.

Example
Results
{ $objectToArray: { item: "foo", qty: 25 } }
[
{
"k" : "item",
"v" : "foo"
},
{
"k" : "qty",
"v" : 25
}
]
{ $objectToArray: {
item: "foo",
qty: 25,
size: { len: 25, w: 10, uom: "cm" }
} }
[
{
"k" : "item",
"v" : "foo"
},
{
"k" : "qty",
"v" : 25
},
{
"k" : "size",
"v" : {
"len" : 25,
"w" : 10,
"uom" : "cm"
}
}
]

Consider a inventory collection with the following documents:

{ "_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 $objectToArray to return the dimensions field as an array:

db.inventory.aggregate(
[
{
$project: {
item: 1,
dimensions: { $objectToArray: "$dimensions" }
}
}
]
)

The operation returns the following:

{ "_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" } ] }

Consider a inventory collection with the following documents:

{ "_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 $objectToArray along with $unwind and $group to calculate the total items in stock per warehouse.

db.inventory.aggregate([
{ $project: { warehouses: { $objectToArray: "$instock" } } },
{ $unwind: "$warehouses" },
{ $group: { _id: "$warehouses.k", total: { $sum: "$warehouses.v" } } }
])

The operation returns the following:

{ "_id" : "warehouse3", "total" : 200 }
{ "_id" : "warehouse2", "total" : 1000 }
{ "_id" : "warehouse1", "total" : 2500 }

Consider a inventory collection with the following documents:

{ "_id" : 1, "item" : "ABC1", instock: { warehouse1: 2500, warehouse2: 500 } }
{ "_id" : 2, "item" : "ABC2", instock: { warehouse2: 500, warehouse3: 200} }

The following aggregation pipeline operation calculates the total in stock for each item and adds to the instock document:

db.inventory.aggregate( [
{ $addFields: { instock: { $objectToArray: "$instock" } } },
{ $addFields: { instock: { $concatArrays: [ "$instock", [ { "k": "total", "v": { $sum: "$instock.v" } } ] ] } } } ,
{ $addFields: { instock: { $arrayToObject: "$instock" } } }
] )

The operation returns the following:

{ "_id" : 1, "item" : "ABC1", "instock" : { "warehouse1" : 2500, "warehouse2" : 500, "total" : 3000 } }
{ "_id" : 2, "item" : "ABC2", "instock" : { "warehouse2" : 500, "warehouse3" : 200, "total" : 700 } }
Tip
See also:
Give Feedback

On this page

  • Definition
  • Behavior
  • Examples