Docs HomeMongoDB Manual

$objectToArray (aggregation)

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 and v:返回数组中的每个元素都是一个包含两个字段kv的文档:

  • The k field contains the field name in the original document.k字段包含原始文档中的字段名称。
  • The 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.有关表达式的详细信息,请参阅表达式

Behavior行为

For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

Example示例Results结果
{ $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"
}
}
]

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:以下聚合管道操作使用$objectToArraydimensions字段作为数组返回:

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 } }
Tip

See also: 另请参阅:

$arrayToObject