$arrayToObject (aggregation)
On this page本页内容
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
andv
where:k
和v
的文档数组,其中:Thek
field contains the field name.k
字段包含字段名称。Thev
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,从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.$arrayToObject
使用该字段的最后一个值。对于4.0.0-4.0.4,使用的值取决于驱动程序。Starting in 3.6.10,从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.$arrayToObject
使用该字段的最后一个值。对于3.6.0-3.6.9,使用的值取决于驱动器。Starting in 3.4.19,从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.$arrayToObject
使用该字段的最后一个值。对于3.4.0-3.4.19,使用的值取决于驱动程序。
{ $arrayToObject: [ | qty and value 25 , $arrayToObject returns: qty 和值为25 的文档,$arrayToObject 返回:{ "item" : "abc123", "qty" : 25 } |
{ $arrayToObject: { $literal: [ |
{ "item" : "abc123", "qty" : 25 } |
{ $arrayToObject: { $literal: [ |
{ "item" : "abc123" } $arrayToObject uses the last value for that field. $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:$arrayToObject
将dimensions
字段作为文档返回:
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, 从4.0.5+版本(3.6.10+和3.4.19+)开始,如果字段名称在数组中重复,$arrayToObject
uses the last value for that field.$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 } }