$arrayToObject (aggregation)

On this page本页内容

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:包含两个字段kv的文档数组,其中:

    • The k field contains the field name.k字段包含字段名。
    • The v field contains the value of the field.v字段包含该字段的值。
    [ { "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.<expression>可以是解析为两个元素数组的数组或包含“k”和“v”字段的文档数组的任何有效表达式

For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

Behavior行为

If the name of a field repeats in the array,如果字段的名称在数组中重复,

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" : "abc123" }

Starting in versions 4.0.5+ (3.6.10+ and 3.4.19+), if the name of a field repeats in the array, $arrayToObject uses the last value for that field.从版本4.0.5+(3.6.10+和3.4.19+)开始,如果某个字段的名称在数组中重复,$arrayToObject将使用该字段的最后一个值。

Examples示例

$arrayToObject Example示例

Consider a inventory collection with the following documents:考虑一个inventory集合带有以下文档:

{ "_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:以下聚合管道操作使用$arrayToObjectdimensions字段作为文档返回:

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

Starting in versions 4.0.5+ (3.6.10+ and 3.4.19+), if the name of a field repeats in the array, $arrayToObject uses the last value for that field.从版本4.0.5+(3.6.10+和3.4.19+)开始,如果某个字段的名称在数组中重复,$arrayToObject将使用该字段的最后一个值。

$objectToArray + $arrayToObject Example

Consider a inventory collection with the following documents:考虑一个inventory集合带有以下文档:

{ "_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:以下聚合管道操作计算每个项目的总库存,并将其添加到instock文档中:

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