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, 在MongoDB 5.0及更高版本中,"a.2" is processed before "a.10" because 2 comes before 10 in numeric order."a.2"在"a.10"之前处理,因为2按数字顺序排在10之前。
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更新。 |
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.有关语法和示例,请参阅使用聚合管道进行更新。