Definition定义
$objectToArrayConverts a document to an array. The return array contains an element for each field/value pair in the original document. Each element in the return array is a document that contains two fields将文档转换为数组。返回数组包含原始文档中每个字段/值对的元素。返回数组中的每个元素都是一个文档,其中包含两个字段kandv:k和v:Thekfield contains the field name in the original document.k字段包含原始文档中的字段名称。Thevfield contains the value of the field in the original document.v字段包含原始文档中该字段的值。
$objectToArrayhas the following syntax:具有以下语法:{ $objectToArray: <object> }The<object>expression can be any valid expression as long as it resolves to a document object.<object>表达式可以是任何有效的表达式,只要它解析为文档对象即可。$objectToArrayapplies to the top-level fields of its argument. If the argument is a document that itself contains embedded document fields, the$objectToArraydoes not recursively apply to the embedded document fields.$objectToArray应用于其参数的顶级字段。如果参数是本身包含嵌入式文档字段的文档,则$objectToArray不会递归应用于嵌入式文档字段。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
Behavior行为
For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
|
|
|
|
Examples示例
$objectToArray Example示例
Consider a 考虑使用以下文件进行inventory collection with the following documents:inventory集合:
db.inventory.insertMany( [
{ _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: "XYZ1", dimensions: { l: 70, w: 75, uom: "cm" } }
] )
The following aggregation pipeline operation use the 以下聚合管道操作使用$objectToArray to return the dimensions field as an array:$objectToArray将dimensions字段作为数组返回:
db.inventory.aggregate(
[
{
$project: {
item: 1,
dimensions: { $objectToArray: "$dimensions" }
}
}
]
)
The operation returns the following:该操作返回以下内容:
{ _id: 1, item: "ABC1", dimensions: [ { "k" : "l", "v" : 25 }, { "k" : "w", "v" : 10 }, { "k" : "uom", "v" : "cm" } ] }
{ _id: 2, item: "ABC2", dimensions: [ { "k" : "l", "v" : 50 }, { "k" : "w", "v" : 25 }, { "k" : "uom", "v" : "cm" } ] }
{ _id: 3, item: "XYZ1", dimensions: [ { "k" : "l", "v" : 70 }, { "k" : "w", "v" : 75 }, { "k" : "uom", "v" : "cm" } ] }$objectToArray to Sum Nested Fields对嵌套字段求和
Consider a 考虑使用以下文件进行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 uses the 以下聚合管道操作使用$objectToArray along with $unwind and $group to calculate the total items in stock per warehouse.$objectToArray以及$unflew和$group来计算每个仓库的inventory总量。
db.inventory.aggregate([
{ $project: { warehouses: { $objectToArray: "$instock" } } },
{ $unwind: "$warehouses" },
{ $group: { _id: "$warehouses.k", total: { $sum: "$warehouses.v" } } }
])
The operation returns the following:该操作返回以下内容:
{ _id: "warehouse3", total: 200 }
{ _id: "warehouse2", total: 1000 }
{ _id: "warehouse1", total: 2500 }$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:inventory,并添加到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 } }