$mergeObjects (aggregation)
On this page本页内容
Definition定义
$mergeObjects
Combines multiple documents into a single document.将多个文档合并为一个文档。
$mergeObjects
is available in these stages:可在以下阶段使用:
Syntax语法
When used as a 当用作$bucket
, $bucketAuto
, or $group
stage accumulator, $mergeObjects
has this syntax:$bucket
、$bucketAuto
或$group
阶段累加器时,$mergeObjects
具有以下语法:
{ $mergeObjects: <document> }
When used in other expressions (including in 当在其他表达式(包括$bucket
, $bucketAuto
, and $group
stages) but not as an accumulator, $mergeObjects
has this syntax:$bucket
、$bucketAuto
和$group
阶段)中使用而不是作为累加器时,$mergeObjects
具有以下语法:
{ $mergeObjects: [ <document1>, <document2>, ... ] }
The <document>
can be any valid expression that resolves to a document.<document>
可以是解析为文档的任何有效表达式。
Behavior行为
$mergeObjects
ignores 忽略null
operands. null
运算数。If all the operands to 如果$mergeObjects
resolves to null, $mergeObjects
returns an empty document { }
.$mergeObjects
的所有操作数解析为null
,$mergeObjects
将返回一个空文档{ }
。
$mergeObjects
overwrites the field values as it merges the documents. 在合并文档时覆盖字段值。If documents to merge include the same field name, the field, in the resulting document, has the value from the last document merged for the field.如果要合并的文档包含相同的字段名称,则在生成的文档中,该字段的值将来自为该字段合并的最后一个文档。
{ $mergeObjects: [ { a: 1 }, null ] } |
{ a: 1 } |
{ $mergeObjects: [ null, null ] } |
{ } |
{ |
{ a: 3, b: 2, c: 3 } |
{ |
{ a: 3, b: null, c: 3 } |
Examples实例
$mergeObjects
Create a collection 使用以下文档创建集合orders
with the following documents:orders
:
db.orders.insertMany( [
{ "_id" : 1, "item" : "abc", "price" : 12, "ordered" : 2 },
{ "_id" : 2, "item" : "jkl", "price" : 20, "ordered" : 1 }
] )
Create another collection 使用以下文档创建另一个集合items
with the following documents:items
:
db.items.insertMany( [
{ "_id" : 1, "item" : "abc", description: "product 1", "instock" : 120 },
{ "_id" : 2, "item" : "def", description: "product 2", "instock" : 80 },
{ "_id" : 3, "item" : "jkl", description: "product 3", "instock" : 60 }
] )
The following operation first uses the 以下操作首先使用$lookup
stage to join the two collections by the item
fields and then uses $mergeObjects
in the $replaceRoot
to merge the joined documents from items
and orders
:$lookup
阶段按项目字段联接两个集合,然后使用$replaceRoot
中的$mergeObjects
合并来自items
和orders
的联接文档:
db.orders.aggregate( [
{
$lookup: {
from: "items",
localField: "item", // field in the orders collection
foreignField: "item", // field in the items collection
as: "fromItems"
}
},
{
$replaceRoot: { newRoot: { $mergeObjects: [ { $arrayElemAt: [ "$fromItems", 0 ] }, "$$ROOT" ] } }
},
{ $project: { fromItems: 0 } }
] )
The operation returns the following documents:该操作返回以下文档:
{
_id: 1,
item: 'abc',
description: 'product 1',
instock: 120,
price: 12,
ordered: 2
},
{
_id: 2,
item: 'jkl',
description: 'product 3',
instock: 60,
price: 20,
ordered: 1
}
$mergeObjects
as an Accumulator作为累加器
Create a collection 使用以下文档创建集合sales
with the following documents:sales
:
db.sales.insertMany( [
{ _id: 1, year: 2017, item: "A", quantity: { "2017Q1": 500, "2017Q2": 500 } },
{ _id: 2, year: 2016, item: "A", quantity: { "2016Q1": 400, "2016Q2": 300, "2016Q3": 0, "2016Q4": 0 } } ,
{ _id: 3, year: 2017, item: "B", quantity: { "2017Q1": 300 } },
{ _id: 4, year: 2016, item: "B", quantity: { "2016Q3": 100, "2016Q4": 250 } }
] )
The following operation uses 以下操作使用$mergeObjects
as a accumulator in a $group
stage that groups documents by the item
field:$mergeObjects
作为$group
阶段中的累加器,该阶段按item
字段对文档进行分组:
When used as an accumulator, 当用作累加器时,$mergeObjects
operator accepts a single operand.$mergeObjects
运算符接受单个操作数。
db.sales.aggregate( [
{ $group: { _id: "$item", mergedSales: { $mergeObjects: "$quantity" } } }
] )
The operation returns the following documents:该操作返回以下文档:
{
_id: 'A',
mergedSales: { '2017Q1': 500, '2017Q2': 500, '2016Q1': 400, '2016Q2': 300, '2016Q3': 0, '2016Q4': 0 }
},
{
_id: 'B',
mergedSales: { '2017Q1': 300, '2016Q3': 100, '2016Q4': 250 }
}
If the documents to merge include the same field name, the field in the resulting document has the value from the last document merged for the field.如果要合并的文档包含相同的字段名称,则结果文档中的字段将具有该字段的上一个合并文档中的值。