Navigation
This version of the documentation is archived and no longer supported.

$arrayToObject (aggregation)

Definition

$arrayToObject

New in version 3.4.4.

Converts an array into a single document; the array must be either:

  • An array of two-element arrays where the first element is the field name, and the second element is the field value:

    [ [ "item", "abc123"], [ "qty", 25 ] ]
    

- OR -

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

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

$arrayToObject has the following syntax:

{ $arrayToObject: <expression> }

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

For more information on expressions, see Expressions.

Behavior

If the name of a field repeats in the array,

  • Starting in 3.4.19, $arrayToObject uses the last value for that field. For 3.4.4-3.4.19, the value uses depends on the driver.
Example Results
{ $arrayToObject:  { $literal: [
      { "k": "item", "v": "abc123"},
      { "k": "qty", "v": 25 }
] } }
{ "item" : "abc123", "qty" : 25 }
{ $arrayToObject: { $literal:  [
   [ "item", "abc123"], [ "qty", 25 ]
] } }
{ "item" : "abc123", "qty" : 25 }
{ $arrayToObject: { $literal: [
   { "k": "item", "v": "123abc"},
   { "k": "item", "v": "abc123" }
] } }
{ "item" : "123abc" }

For MongoDB 3.4, starting in 3.4.19+, if the name of a field repeats in the array, $arrayToObject uses the last value for that field.

Examples

$arrayToObject Example

Consider a inventory collection with the following documents:

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

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

The operation returns the following:

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

For MongoDB 3.4, starting in 3.4.19+, if the name of a field repeats in the array, $arrayToObject uses the last value for that field.

$objectToArray + $arrayToObject Example

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 } }

See also

$objectToArray