Definition定义
$arrayToObjectConverts an array into a single document.将数组转换为单个文档。$arrayToObjecthas the following syntax:具有以下语法:{ $arrayToObject: <expression> }The<expression>can be any valid expression that resolves to:<expression>可以是解析为以下内容的任何有效表达式:An array of two-element arrays where the first element is the field name, and the second element is the field value:一个由两个元素组成的数组,其中第一个元素是字段名,第二个元素是域值:$arrayToObject: [
[ [ "item", "abc123" ], [ "qty", 25 ] ]
]
- OR -
An array of documents that contains two fields,包含两个字段kandvwhere:k和v的文档数组,其中:Thekfield contains the field name.k字段包含字段名。Thevfield contains the value of the field.v字段包含字段的值。
$arrayToObject: [
[
{ "k": "item", "v": "abc123" },
{ "k": "qty", "v": 25 }
]
]
Behavior行为
If you pass in an empty array to 如果你向$arrayToObject, MongoDB creates an empty object.$arrayToObject传递一个空数组,MongoDB会创建一个空对象。
If the name of a field repeats in the array,如果字段的名称在数组中重复,
Starting in 4.0.5,从4.0.5开始,$arrayToObjectuses 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开始,$arrayToObjectuses 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开始,$arrayToObjectuses 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,使用的值取决于驾驶员。
|
|
|
|
|
|
Examples示例
$arrayToObject Example示例
Consider a 考虑使用以下文件进行inventory collection with the following documents:inventory集合:
db.inventory.insertMany( [
{ _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将维度字段作为文档返回:
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 an 考虑使用以下文件进行inventory collection with the following documents:inventory集合:
db.inventory.insertMany( [
{ _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 } }