Starting in MongoDB 4.2, you can use the aggregation pipeline for update operations. 从MongoDB 4.2开始,您可以使用聚合管道进行更新操作。With the update operations, the aggregation pipeline can consist of the following stages:通过更新操作,聚合管道可以由以下阶段组成:
$addFields |
$set |
$project |
$unset |
$replaceRoot |
$replaceWith |
Using the aggregation pipeline allows for a more expressive update statement, such as expressing conditional updates based on current field values or updating one field using the value of another field(s).使用聚合管道允许更具表现力的update语句,例如基于当前字段值表示条件更新,或者使用另一个字段的值更新一个字段。
You can try out the example in the provided shell. 您可以在提供的shell中尝试该示例。Click inside the shell to connect. 在外壳内部单击以进行连接。Once connected, you can run the examples in the shell.连接后,可以在shell中运行示例。
Create an example 创建示例students
collection (if the collection does not currently exist, insert operations will create the collection):students
集合(如果该集合当前不存在,则插入操作将创建该集合):
To verify, query the collection:要验证,请查询集合:
The following 以下db.collection.updateOne()
operation uses an aggregation pipeline to update the document with _id: 3
:db.collection.updateOne()
操作使用聚合管道更新_id
为3
的文档:
Specifically, the pipeline consists of a 具体地说,管道由$set
stage which adds the test3
field (and sets its value to 98
) to the document and sets the modified
field to the current datetime. $set
阶段组成,它将test3
字段(并将其值设置为98
)添加到文档中,并将修改后的字段设置为当前日期时间。For the current datetime, the operation uses the aggregation variable 对于当前日期时间,该操作将使用聚合变量NOW
for the (to access the variable, prefix with $$
and enclose in quotes).NOW
(要访问该变量,请使用$$
前缀并用引号括起来)。
To verify the update, you can query the collection:要验证更新,可以查询集合:
You can try out the examples in the provided shell. 您可以在提供的shell中尝试这些示例。Click inside the shell to connect. Once connected, you can run the examples in the shell.在外壳内部单击以进行连接。连接后,可以在shell中运行示例。
Create an example 创建示例students2
collection (if the collection does not currently exist, insert operations will create the collection):students2
集合(如果该集合当前不存在,则插入操作将创建该集合):
To verify, query the collection:要验证,请查询集合:
The following 以下db.collection.updateMany()
operation uses an aggregation pipeline to standardize the fields for the documents (i.e. documents in the collection should have the same fields) and update the modified
field:db.collection.updateMany()
操作使用聚合管道来标准化文档的字段(即集合中的文档应具有相同的字段)并更新modified
字段:
Specifically, the pipeline consists of:具体而言,管道包括:
$replaceRoot
stage with an $mergeObjects
expression to set default values for the quiz1
, quiz2
, test1
and test2
fields. $mergeObjects
表达式的$replaceRoot
阶段,用于设置quiz1
、quiz2
、test1
和test2
字段的默认值。ROOT
refers to the current document being modified (to access the variable, prefix with $$
and enclose in quotes). ROOT
表示正在修改的当前文档(要访问该变量,请以$$
作为前缀并用引号括起来)。$set
stage to update the modified
field to the current datetime. $set
阶段,用于将modified
字段更新为当前日期时间。NOW
for the (to access the variable, prefix with $$
and enclose in quotes).NOW
(要访问该变量,请使用$$
前缀并用引号括起来)。To verify the update, you can query the collection:要验证更新,可以查询集合:
You can try out the examples in the provided shell. 您可以在提供的shell中尝试这些示例。Click inside the shell to connect. 在外壳内部单击以进行连接。Once connected, you can run the examples in the shell.连接后,可以在shell中运行示例。
Create an example 创建示例students3
collection (if the collection does not currently exist, insert operations will create the collection):students3
集合(如果该集合当前不存在,则插入操作将创建该集合):
To verify, query the collection:要验证,请查询集合:
The following 下面的db.collection.updateMany()
operation uses an aggregation pipeline to update the documents with the calculated grade average and letter grade.db.collection.updateMany()
操作使用聚合管道使用计算的平均等级和字母等级更新文档。
Specifically, the pipeline consists of:具体而言,管道包括:
$set
stage to calculate the truncated average value of the tests
array elements and to update the modified
field to the current datetime. $set
阶段,用于计算test
数组元素的截断平均值,并将modified
字段更新为当前日期时间。$avg
and $trunc
expressions. $avg
和$trunc
表达式。NOW
for the (to access the variable, prefix with $$
and enclose in quotes)NOW
(要访问该变量,请使用$$
前缀并用引号括起来)$set
stage to add the grade
field based on the average
using the $switch
expression.$set
阶段,用于使用$switch
表达式基于average
添加grade
字段。To verify the update, you can query the collection:要验证更新,可以查询集合:
You can try out the examples in the provided shell. 您可以在提供的shell中尝试这些示例。Click inside the shell to connect. Once connected, you can run the examples in the shell.在外壳内部单击以进行连接。连接后,可以在shell中运行示例。
Create an example 创建示例students4
collection (if the collection does not currently exist, insert operations will create the collection):students4
集合(如果该集合当前不存在,则插入操作将创建该集合):
To verify, query the collection:要验证,请查询集合:
The following 以下db.collection.updateOne()
operation uses an aggregation pipeline to add quiz scores to the document with _id: 2
:db.collection.updateOne()
操作使用聚合管道将测验分数添加到_id: 2
的文档中:
To verify the update, query the collection:要验证更新,请查询集合:
You can try out the examples in the provided shell. 您可以在提供的shell中尝试这些示例。Click inside the shell to connect. Once connected, you can run the examples in the shell.在外壳内部单击以进行连接。连接后,可以在shell中运行示例。
Create an example 创建包含摄氏温度的示例temperatures
collection that contains temperatures in Celsius (if the collection does not currently exist, insert operations will create the collection):temperatures
集合(如果该集合当前不存在,则插入操作将创建该集合):
To verify, query the collection:要验证,请查询集合:
The following 以下db.collection.updateMany()
operation uses an aggregation pipeline to update the documents with the corresponding temperatures in Fahrenheit:db.collection.updateMany()
操作使用聚合管道以相应的华氏温度更新文档:
Specifically, the pipeline consists of an 具体来说,管道包含一个$addFields
stage to add a new array field tempsF
that contains the temperatures in Fahrenheit. $addFields
阶段,用于添加一个新的数组字段tempsF,其中包含以华氏温度为单位的温度。To convert each celsius temperature in the 要将tempsC
array to Fahrenheit, the stage uses the $map
expression with $add
and $multiply
expressions.tempsC
数组中的每个摄氏温度转换为华氏温度,阶段使用带有$add
和$multiply
表达式的$map
表达式。
To verify the update, you can query the collection:要验证更新,可以查询集合:
See also the various update method pages for additional examples:有关其他示例,请参阅各种更新方法页面: