Docs HomeMongoDB Manual

Update Operators更新运算符

On this page本页内容

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

Specify the operator expression in a document of the form:在以下形式的文档中指定运算符表达式:

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

For details on a specific operator, including syntax and examples, click on the link to the operator's reference page.有关特定运算符的详细信息,包括语法和示例,请单击该运算符参考页的链接。

Update Operators更新运算符

Behavior行为

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

In MongoDB 4.4 and earlier, update operators process all document fields in lexicographic order.在MongoDB 4.4及更早版本中,update运算符按字典顺序处理所有文档字段。

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之前。

In MongoDB 4.4 and earlier, "a.10" is processed before "a.2" because 10 comes before 2 in lexicographic order.在MongoDB 4.4及更早版本中,"a.10""a.2"之前处理,因为按照字典顺序,102之前。

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更新。