Docs HomeMongoDB Manual

$arrayToObject (aggregation)

Definition定义

$arrayToObject

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,如果字段的名称在数组中重复,

  • Starting in 4.0.5, $arrayToObject uses the last value for that field. For 4.0.0-4.0.4, the value used depends on the driver.从4.0.5开始,$arrayToObject使用该字段的最后一个值。对于4.0.0-4.0.4,使用的值取决于驱动程序。
  • Starting in 3.6.10, $arrayToObject uses the last value for that field. For 3.6.0-3.6.9, the value used depends on the driver.从3.6.10开始,$arrayToObject使用该字段的最后一个值。对于3.6.0-3.6.9,使用的值取决于驱动器。
  • Starting in 3.4.19, $arrayToObject uses the last value for that field. For 3.4.0-3.4.19, the value uses depends on the driver.从3.4.19开始,$arrayToObject使用该字段的最后一个值。对于3.4.0-3.4.19,使用的值取决于驱动程序。
Example示例Results结果
{ $arrayToObject: [
[ { "k": "item", "v": "abc123" },
{ "k": "qty", "v": "$qty" } ]
] }
Given a document with the field qty and value 25, $arrayToObject returns: 给定字段qty和值为25的文档,$arrayToObject返回:
{ "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:以下聚合管道操作计算每个项目的inventory总额,并添加到inventory文档中:

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: 另请参阅:

$objectToArray