$arrayToObject (aggregation)

Definition

  • $arrayToObject

New in version 3.4.4.

Converts an array into a single document; the array must beeither:

  • An array of two-element arrays where the first element is thefield name, and the second element is the field value:
  1. [ [ "item", "abc123"], [ "qty", 25 ] ]
  • OR -

    • An array of documents that contains two fields, k and vwhere:

      • The k field contains the field name.
      • The v field contains the value of the field.
  1. [ { "k": "item", "v": "abc123"}, { "k": "qty", "v": 25 } ]

$arrayToObject has the following syntax:

  1. { $arrayToObject: <expression> }

The <expression> can be any valid expression that resolves to an array of two-elementarrays or array of documents that contains “k” and “v” fields.

For more information on expressions, seeExpressions.

Behavior

If the name of a field repeats in the array,

  • Starting in 4.0.5, $arrayToObject uses the last valuefor that field. For 4.0.0-4.0.4, the value used depends on the driver.
  • Starting in 3.6.10, $arrayToObject uses the last valuefor that field. For 3.6.0-3.6.9, the value used depends on the driver.
  • Starting in 3.4.19, $arrayToObject uses the last valuefor that field. For 3.4.0-3.4.19, the value uses depends on thedriver.
ExampleResults
  1. { $arrayToObject: { $literal: [ { "k": "item", "v": "abc123"}, { "k": "qty", "v": 25 }] } }
  1. { "item" : "abc123", "qty" : 25 }
  1. { $arrayToObject: { $literal: [ [ "item", "abc123"], [ "qty", 25 ]] } }
  1. { "item" : "abc123", "qty" : 25 }
  1. { $arrayToObject: { $literal: [ { "k": "item", "v": "123abc"}, { "k": "item", "v": "abc123" }] } }
  1. { "item" : "abc123" }
Starting in versions 4.0.5+ (3.6.10+ and 3.4.19+), if the nameof a field repeats in the array, $arrayToObjectuses the last value for that field.

Examples

$arrayToObject Example

Consider a inventory collection with the following documents:

  1. { "_id" : 1, "item" : "ABC1", dimensions: [ { "k": "l", "v": 25} , { "k": "w", "v": 10 }, { "k": "uom", "v": "cm" } ] }
  2. { "_id" : 2, "item" : "ABC2", dimensions: [ [ "l", 50 ], [ "w", 25 ], [ "uom", "cm" ] ] }
  3. { "_id" : 3, "item" : "ABC3", dimensions: [ [ "l", 25 ], [ "l", "cm" ], [ "l", 50 ] ] }

The following aggregation pipeline operation use the$arrayToObject to return the dimensions field as adocument:

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

The operation returns the following:

  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" : "ABC3", "dimensions" : { "l" : 50 } }

Starting in versions 4.0.5+ (3.6.10+ and 3.4.19+), if the name of afield repeats in the array, $arrayToObject uses the lastvalue for that field.

$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

$objectToArray