$objectToArray (aggregation)

Definition

  • $objectToArray

New in version 3.4.4.

Converts a document to an array. The return array contains anelement for each field/value pair in the original document.Each element in the return array is a document that contains twofields 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 originaldocument.$objectToArray has the following syntax:
  1. { $objectToArray: <object> }

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

For more information on expressions, seeExpressions.

Behavior

For more information on expressions, see Expressions.

ExampleResults
  1. { $objectToArray: { item: "foo", qty: 25 } }
  1. [ { "k" : "item", "v" : "foo" }, { "k" : "qty", "v" : 25 }]
  1. { $objectToArray: { item: "foo", qty: 25, size: { len: 25, w: 10, uom: "cm" } } }
  1. [ { "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:

  1. { "_id" : 1, "item" : "ABC1", dimensions: { l: 25, w: 10, uom: "cm" } }
  2. { "_id" : 2, "item" : "ABC2", dimensions: { l: 50, w: 25, uom: "cm" } }
  3. { "_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:

  1. db.inventory.aggregate(
  2. [
  3. {
  4. $project: {
  5. item: 1,
  6. dimensions: { $objectToArray: "$dimensions" }
  7. }
  8. }
  9. ]
  10. )

The operation returns the following:

  1. { "_id" : 1, "item" : "ABC1", "dimensions" : [ { "k" : "l", "v" : 25 }, { "k" : "w", "v" : 10 }, { "k" : "uom", "v" : "cm" } ] }
  2. { "_id" : 2, "item" : "ABC2", "dimensions" : [ { "k" : "l", "v" : 50 }, { "k" : "w", "v" : 25 }, { "k" : "uom", "v" : "cm" } ] }
  3. { "_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:

  1. { "_id" : 1, "item" : "ABC1", instock: { warehouse1: 2500, warehouse2: 500 } }
  2. { "_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.

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

The operation returns the following:

  1. { "_id" : "warehouse3", "total" : 200 }
  2. { "_id" : "warehouse2", "total" : 1000 }
  3. { "_id" : "warehouse1", "total" : 2500 }

$objectToArray + $arrayToObject Example

Consider a inventory collection with the following documents:

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

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

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

The operation returns the following:

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

See also

$arrayToObject