$objectToArray (aggregation)
On this page本页内容
Definition定义
$objectToArray
-
Converts 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返回数组中的每个元素都是一个包含两个字段k
andv
:k
和v
的文档:Thek
field contains the field name in the original document.k
字段包含原始文档中的字段名称。Thev
field contains the value of the field in the original document.v
字段包含原始文档中字段的值。
$objectToArray
has the following syntax:具有以下语法:{ $objectToArray: <object> }
The<object>
expression can be any valid expression as long as it resolves to a document object.<object>
表达式可以是任何有效的表达式,只要它解析为文档对象即可。$objectToArray
applies to the top-level fields of its argument.应用于其参数的顶级字段。If the argument is a document that itself contains embedded document fields, the如果参数是一个本身包含嵌入文档字段的文档,则$objectToArray
does not recursively apply to the embedded document fields.$objectToArray
不会递归应用于嵌入文档字段。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
Behavior行为
For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
{ $objectToArray: { item: "foo", qty: 25 } } |
[ |
{ $objectToArray: { |
[ |
Examples实例
$objectToArray
Example示例
Consider a 考虑一个包含以下文档的inventory
collection with the following documents:inventory
集合:
{ "_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
集合:
{ "_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
以及$unwind
和$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 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 } }