On this page本页内容
$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
and v
:k
和v
的文档:
k
field contains the field name in the original document.k
字段包含原始文档中的字段名。v
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.有关表达式的详细信息,请参阅表达式。
For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
{ $objectToArray: { item: "foo", qty: 25 } } | [ { "k" : "item", "v" : "foo" }, { "k" : "qty", "v" : 25 } ] |
{ $objectToArray: { item: "foo", qty: 25, size: { len: 25, w: 10, uom: "cm" } } } | [ { "k" : "item", "v" : "foo" }, { "k" : "qty", "v" : 25 }, { "k" : "size", "v" : { "len" : 25, "w" : 10, "uom" : "cm" } } ] |
$objectToArray
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
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
来计算每个仓库的库存总量。
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
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 } }