Database Manual / Reference / Query Language

Update Operators更新运算符

The following modifiers are available for use in update operations such as db.collection.updateMany() and db.collection.findAndModify().以下修饰符可用于更新操作,如db.collection.updateMany()db.collection.findAndModify()

Compatibility兼容性

You can use update operators for deployments hosted in the following environments:您可以对以下环境中托管的部署使用更新运算符:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

Specify update operators in the following form:按以下格式指定更新运算符:

{
<operator1>: { <field1>: <value1>, ... },
<operator2>: { <field2>: <value2>, ... },
...
}

Update Operators更新运算符

Behavior行为

Starting in MongoDB 5.0, update operators process document fields with string-based names in lexicographic order. Fields with numeric names are processed in numeric order.从MongoDB 5.0开始,更新运算符按字典顺序处理具有基于字符串的名称的文档字段。具有数字名称的字段按数字顺序处理。

Consider this example $set command:考虑以下示例$set命令:

{ $set: { "a.2": <new value>, "a.10": <new value>, } }

In MongoDB 5.0 and later, "a.2" is processed before "a.10" because 2 comes before 10 in numeric order.在MongoDB 5.0及更高版本中,"a.2""a.10"之前处理,因为2按数字顺序排在10之前。

Fields字段

Name名称Description描述
$currentDateSets the value of a field to current date, either as a Date or a Timestamp.将字段的值设置为当前日期,可以是日期或时间戳。
$incIncrements the value of the field by the specified amount.将字段的值增加指定的量。
$minOnly updates the field if the specified value is less than the existing field value.仅当指定值小于现有字段值时才更新字段。
$maxOnly updates the field if the specified value is greater than the existing field value.仅当指定值大于现有字段值时才更新字段。
$mulMultiplies the value of the field by the specified amount.将字段的值乘以指定的金额。
$renameRenames a field.重命名字段。
$setSets the value of a field in a document.设置文档中字段的值。
$setOnInsertSets the value of a field if an update results in an insert of a document. Has no effect on update operations that modify existing documents.如果更新导致插入文档,则设置字段的值。对修改现有文档的更新操作没有影响。
$unsetRemoves the specified field from a document.从文档中删除指定字段。

Array数组

Operators运算符

Name名称Description描述
$Acts as a placeholder to update the first element that matches the query condition.充当占位符,更新与查询条件匹配的第一个元素。
$[]Acts as a placeholder to update all elements in an array for the documents that match the query condition.充当占位符,更新与查询条件匹配的文档的数组中的所有元素。
$[<identifier>]Acts as a placeholder to update all elements that match the arrayFilters condition for the documents that match the query condition.充当占位符,更新与查询条件匹配的文档中与arrayFilters条件匹配的所有元素。
$addToSetAdds elements to an array only if they do not already exist in the set.仅当元素在集合中不存在时,才向数组添加元素。
$popRemoves the first or last item of an array.删除数组的第一项或最后一项。
$pullRemoves all array elements that match a specified query.删除与指定查询匹配的所有数组元素。
$pushAdds an item to an array.将项添加到数组中。
$pullAllRemoves all matching values from an array.从数组中删除所有匹配的值。

Modifiers修饰符

Name名称Description描述
$eachModifies the $push and $addToSet operators to append multiple items for array updates.修改$push$addToSet运算符,以附加多个项进行数组更新。
$positionModifies the $push operator to specify the position in the array to add elements.修改$push运算符以指定在数组中添加元素的位置。
$sliceModifies the $push operator to limit the size of updated arrays.修改$push运算符以限制更新数组的大小。
$sortModifies the $push operator to reorder documents stored in an array.修改$push运算符以重新排序存储在数组中的文档。

Bitwise按位

Name名称Description描述
$bitPerforms bitwise AND, OR, and XOR updates of integer values.执行整数值的逐位ANDORXOR更新。

Updates with Aggregation Pipeline聚合管道的更新

To create expressive update statements, such as conditional updates based on current field values or single-field updates that use the value of another field, you can perform updates with an aggregation pipeline.要创建富有表现力的更新语句,例如基于当前字段值的条件更新或使用另一个字段值的单个字段更新,您可以使用聚合管道执行更新。

For syntax and examples, see Updates with Aggregation Pipeline.有关语法和示例,请参阅使用聚合管道进行更新