On this page本页内容
$replaceWith
¶New in version 4.2.版本4.2中的新功能。
Replaces the input document with the specified document. 用指定的文档替换输入文档。The operation replaces all existing fields in the input document, including the 该操作将替换输入文档中的所有现有字段,包括_id
field. _id
字段。With 使用$replaceWith
, you can promote an embedded document to the top-level. $replaceWith
,可以将嵌入式文档升级到顶层。You can also specify a new document as the replacement.您还可以指定一个新文档作为替换文档。
The $replaceWith
is an alias for $replaceRoot
.$replaceWith
是$replaceRoot
的别名。
The $replaceWith
stage has the following form:$replaceWith
阶段具有以下形式:
The replacement document can be any valid expression that resolves to a document. 替换文档可以是解析为文档的任何有效表达式。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
If the 如果<replacementDocument>
is not a document, $replaceWith
errors and fails.<replacementDocument>
不是一个文档,$replaceWith
将出现错误并失败。
If the 如果<replacementDocument>
resolves to a missing document (i.e. the document does not exist), $replaceWith
errors and fails. <replacementDocument>
解析为缺少的文档(即该文档不存在),$replaceWith
将出现错误并失败。For example, create a collection with the following documents:例如,创建包含以下文档的集合:
Then the following 然后,以下$replaceWith
operation fails because one of the document does not have the name
field:$replaceWith
操作失败,因为其中一个文档没有name
字段:
To avoid the error, you can use 为了避免错误,可以使用$mergeObjects
to merge the name
document with some default document; for example:$mergeObjects
将name
文档与一些默认文档合并;例如:
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 $replaceWith
stage:$replaceWith
阶段之前,可以通过包含$match
阶段来检查文档字段是否存在,从而跳过缺少name
字段的文档:
Or, you can use 或者,您可以使用$ifNull
expression to specify some other document to be root; for example:$ifNull
表达式指定其他文档作为根;例如:
$replaceWith
Create a collection named 使用以下文档创建名为people
with the following documents:people
的集合:
The following operation uses the 以下操作使用$replaceWith
stage to replace each input document with the result of a $mergeObjects
operation. $replaceWith
阶段将每个输入文档替换为$mergeObjects
操作的结果。The $mergeObjects
expression merges the specified default document with the pets
document.$mergeObjects
表达式将指定的默认文档与pets
文档合并。
The operation returns the following results:操作返回以下结果:
$replaceWith
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:操作返回以下结果:
$replaceWith
An example collection 示例集合sales
is populated with the following documents:sales
包含以下文档:
Assume that for reporting purposes, you want to calculate for each completed sale, the total amount as of the current report run time. 假设出于报告目的,您希望计算每个已完成销售的截至当前报告运行时的总金额。The following operation finds all the sales with status 以下操作将查找状态为C
and creates new documents using the $replaceWith
stage. C
的所有销售,并使用$replaceWith
阶段创建新文档。The $replaceWith
calculates the total amount as well as uses the variable NOW
to get the current time.$replaceWith
计算总金额,并使用变量NOW
获取当前时间。
The operation returns the following documents:该操作将返回以下文档:
An example collection 示例集合reportedsales
is populated with the reported sales information by quarter and regions:reportedsales
按季度和地区填充报告的销售信息:
Assume that for reporting purposes, you want to view the reported sales data by quarter; e.g.假设出于报告目的,您希望按季度查看报告的销售数据;例如
To view the data grouped by quarter, you can use the following aggregation pipeline:要查看按季度分组的数据,可以使用以下聚合管道:
The $addFields
stage adds a new obj
document field that defines the key k
as the region value and the value v
as the quantity for that region. $addFields
阶段添加了一个新的obj
文档字段,该字段将键k定义为区域值,将值v
定义为该区域的数量。For example:例如:
The $group
stage groups by the quarter and uses $push
to accumulate the obj
fields into a new items
array field. $group
阶段按季度分组,并使用$push
将obj
字段累积到新的项目数组字段中。For example:例如:
The $project
stage uses $concatArrays
to create a new array items2
that includes the _id
info and the elements from the items
array:$project
阶段使用$concatArrays
创建一个新的数组items2
,其中包括_id
信息和items
数组中的元素:
The $replaceWith
uses the $arrayToObject
to convert the items2
into a document, using the specified key k
and value v
pairs and outputs that document to the next stage. $replaceWith
使用$arrayToObject
将items2
转换为文档,使用指定的键k
和值v
对,并将该文档输出到下一阶段。For example:例如:
The aggregation returns the following document:聚合将返回以下文档:
$replaceWith
a New Document Created from $$ROOT
and a Default Document$replaceWith
一个根据$$ROOT
和默认文档创建的新文档¶Create a collection named 使用以下文档创建名为contacts
with the following documents:contacts
的集合:
The following operation uses 以下操作使用$replaceWith
with $mergeObjects
to output current documents with default values for missing fields:$replaceWith
和$mergeObjects
输出当前文档,其中包含缺失字段的默认值:
The aggregation returns the following documents:聚合将返回以下文档: