On this page本页内容
$mergeObjects
¶New in version 3.6.版本3.6中的新功能。
Combines multiple documents into a single document.将多个文档合并为一个文档。
$group
stage accumulator, $mergeObjects
has the following form:$group
阶段累加器时,$mergeObjects
具有以下形式:
$group
stage but not as an accumulator:$group
阶段,但不作为累加器使用:
The <document>
can be any valid expression that resolves to a document.<document>
可以是解析为文档的任何有效表达式。
mergeObjects
ignores null
operands. mergeObjects
忽略null
操作数。mergeObjects
resolves to null, mergeObjects
returns an empty document { }
.mergeObjects
的所有操作数解析为null
,mergeObjects
将返回一个空文档{}
。mergeObjects
overwrites the field values as it merges the documents. mergeObjects
在合并文档时覆盖字段值。$mergeObjects
¶Create a collection 使用以下文档创建集合orders
with the following documents:orders
:
Create another collection 使用以下文档创建另一个集合items
with the following documents:items
:
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
阶段通过item
字段连接两个集合,然后使用$replaceRoot
中的$mergeObjects
合并来自items
和orders
的连接文档:
The operation returns the following documents:该操作将返回以下文档:
$mergeObjects
Create a collection 使用以下文档创建集合sales
with the following documents:sales
:
The following operation uses 以下操作将$mergeObjects
as a accumulator in a $group
stage that groups documents by the item
field:$mergeObjects
用作$group
阶段中的累加器,该阶段按item
字段对文档进行分组:
Note
When used as an accumulator, 当用作累加器时,$mergeObjects
operator accepts a single operand.$mergeObjects
运算符接受单个操作数。
The operation returns the following documents:该操作将返回以下文档:
Note
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.如果要合并的文档包含相同的字段名,则结果文档中的字段具有该字段上一个合并文档的值。