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>, ... },
...
}
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, 在MongoDB 5.0及更高版本中,"a.2"
is processed before "a.10"
because 2
comes before 10
in numeric order."a.2"
在"a.10"
之前处理,因为2
按数字顺序位于10
之前。
In MongoDB 4.4 and earlier, 在MongoDB 4.4及更早版本中,"a.10"
is processed before "a.2"
because 10
comes before 2
in lexicographic order."a.10"
在"a.2"
之前处理,因为按照字典顺序,10
在2
之前。
Fields字段
$currentDate | |
$inc | |
$min | |
$max | |
$mul | |
$rename | |
$set | |
$setOnInsert | |
$unset |
Array数组
Operators运算符
$ | |
$[] | |
$[<identifier>] | arrayFilters condition for the documents that match the query condition.arrayFilters 条件匹配的所有元素。 |
$addToSet | |
$pop | |
$pull | |
$push | |
$pullAll |
Modifiers修饰符
$each | $push and $addToSet operators to append multiple items for array updates.$push 和$addToSet 运算符,以便为数组更新追加多个项。 |
$position | $push operator to specify the position in the array to add elements.$push 运算符以指定要添加元素的数组中的位置。 |
$slice | $push operator to limit the size of updated arrays.$push 运算符以限制更新数组的大小。 |
$sort | $push operator to reorder documents stored in an array.$push 运算符以重新排序存储在数组中的文档。 |
Bitwise按位
$bit | AND , OR , and XOR updates of integer values.AND 、OR 和XOR 更新。 |