On this page本页内容
$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:k
和v
的文档数组,其中:
k
field contains the field name.k
字段包含字段名。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.有关表达式的详细信息,请参阅表达式。
If the name of a field repeats in the array,如果字段的名称在数组中重复,
$arrayToObject
uses the last value for that field. $arrayToObject
使用该字段的最后一个值。$arrayToObject
uses the last value for that field. $arrayToObject
使用该字段的最后一个值。$arrayToObject
uses the last value for that field. $arrayToObject
使用该字段的最后一个值。{ $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" }
|
$arrayToObject
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
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 } }