On this page本页内容
$replaceRoot
¶New in version 3.4.版本3.4中的新功能。
Replaces the input document with the specified document. 用指定的文档替换输入文档。The operation replaces all existing fields in the input document, including the 该操作将替换输入文档中的所有现有字段,包括_id
field. _id
字段。You can promote an existing embedded document to the top level, or create a new document for promotion (see example).您可以将现有的嵌入式文档升级到顶层,或者创建一个新文档进行升级(参见示例)。
Note
Starting in version 4.2, MongoDB adds a new aggregation pipeline stage 从4.2版开始,MongoDB添加了一个新的聚合管道阶段$replaceWith
that is an alias for $replaceRoot
.$replaceWith
,它是$replaceRoot
的别名。
The $replaceRoot
stage has the following form:$replaceRoot
阶段具有以下形式:
The replacement document can be any valid expression that resolves to a document. 替换文档可以是解析为文档的任何有效表达式。The stage errors and fails if 如果<replacementDocument>
is not a document. <replacementDocument>
不是文档,则阶段会出错并失败。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
If the 如果<replacementDocument>
is not a document, $replaceRoot
errors and fails.<replacementDocument>
不是文档,$replaceRoot
会出错并失败。
If the 如果<replacementDocument>
resolves to a missing document (i.e. the document does not exist), $replaceRoot
errors and fails. <replacementDocument>
解析为缺少的文档(即该文档不存在),$replaceRoot
将出错并失败。For example, create a collection with the following documents:例如,创建包含以下文档的集合:
Then the following 然后,以下$replaceRoot
operation fails because one of the documents does not have the name
field:$replaceRoot
操作失败,因为其中一个文档没有name
字段:
To avoid the error, you can use 为了避免错误,可以使用$mergeObjects
to merge the name
document into some default document; for example:$mergeObjects
将名称文档合并到一些默认文档中;例如:
Alternatively, you can skip the documents that are missing the 或者,在将文档传递到name
field by including a $match
stage to check for existence of the document field before passing documents to the $replaceRoot
stage:$replaceRoot
阶段之前,可以通过包含$match
阶段来检查文档字段是否存在,从而跳过缺少name
字段的文档:
Or, you can use 或者,您可以使用$ifNull
expression to specify some other document to be root; for example:$ifNull
表达式指定其他文档作为根;例如:
$replaceRoot
A collection named 名为people
contains the following documents:people
的集合包含以下文档:
The following operation uses the 以下操作使用$replaceRoot
stage to replace each input document with the result of a $mergeObjects
operation. $replaceRoot
阶段将每个输入文档替换为$mergeObjects
操作的结果。The $mergeObjects
expression merges the specified default document with the pets
document.$mergeObjects
表达式将指定的默认文档与pets
文档合并。
The operation returns the following results:操作返回以下结果:
$replaceRoot
A collection named 名为students
contains the following documents:students
的集合包含以下文档:
The following operation promotes the embedded document(s) with the 以下操作将grade
field greater than or equal to 90
to the top level:grade
字段大于或等于90
的嵌入文档提升到顶层:
The operation returns the following results:操作返回以下结果:
$replaceRoot
You can also create new documents as part of the 您还可以创建新文档作为$replaceRoot
stage and use them to replace all the other fields.$replaceRoot
阶段的一部分,并使用它们替换所有其他字段。
A collection named 名为contacts
contains the following documents:contacts
的集合包含以下文档:
The following operation creates a new document out of the 下面的操作使用first_name
and last_name
fields.first_name
和last_name
字段创建一个新文档。
The operation returns the following results:操作返回以下结果:
$replaceRoot
with a New Document Created from $$ROOT
and a Default Document$replaceRoot
,其中包含从$$ROOT
创建的新文档和默认文档¶Create a collection named 使用以下文档创建名为contacts
with the following documents:contacts
的集合:
The following operation uses 以下操作使用$replaceRoot
with $mergeObjects
to output current documents with default values for missing fields:$replaceRoot
和$mergeObjects
来输出当前文档,其中包含缺失字段的默认值:
The aggregation returns the following documents:聚合将返回以下文档: