【MongoDB】修改操作符 Update Operators
文档格式如下
{
<operator1>: { <field1>: <value1>, ... },
<operator2>: { <field2>: <value2>, ... },
...
}
Update Operators
1. 字段操作
Name | Description |
---|---|
$currentDate | Sets the value of a field to current date, either as a Date or a Timestamp. 将字段的值设置为当前日期,可以是 Date 或 Timestamp。 |
$inc | Increments the value of the field by the specified amount. 将字段的值按给定的amount增加 {¥inc{: , : …}} |
$min | Only updates the field if the specified value is less than the existing field value. 仅当指定值小于现有字段值时才更新字段。 |
$max | Only updates the field if the specified value is greater than the existing field value. 仅当指定值大于现有字段值时才更新字段。 |
$mul | Multiplies the value of the field by the specified amount. 将字段的值按给定的amount相乘 |
$rename | Renames a field. 给字段重命名 |
$set | Sets the value of a field in a document. 设置文档中某个字段的值 |
$setOnInsert | Sets 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. 如果update导致文档的insert,则设置字段的值。 对修改现有文档的更新操作没有影响。 |
$unset | Removes the specified field from a document. 从文档中删除指定指定的字段 |
2. 数组修改器
2.1 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. 充当占位符,用于更新数组中与查询条件匹配的文档的所有元素。 |
$[<元素名>] | Acts as a placeholder to update all elements that match the arrayFilters condition for the documents that match the query condition.充当占位符,为与查询条件匹配的文档更新所有与 arrayFilters 条件匹配的元素。 |
$addToSet | Adds elements to an array only if they do not already exist in the set. 如果数组中没有这些元素,那么则向数组中添加这些元素 |
$pop | 删除数组中的第一个或最后一个元素(传值-1:删第一个,传值1:删最后一个) |
$pull | Removes all array elements that match a specified query 移除所有匹配指定查询的数组元素 |
$push | Adds an item to an array. 添加指定的值到数组中 |
$pushAll | Removes all matching values from an array. 在数组中删除所有匹配的值 |
2.2 Modifiers
Name | Description |
---|---|
$each | Modifies the push and addToSet operators to append multiple items for array updates. 修改 push 和 addToSet 运算符以附加多个元素以进行数组更新。 |
$position | Modifies the push operator to specify the position in the array to add elements. 修改 push 操作符以指定在数组中的位置添加元素。 |
$slice | Modifies the push operator to limit the size of updated arrays. 修改 push 操作符以限制更新数组的大小。 |
$sort | Modifies the push operator to reorder documents stored in an array. 修改 push 操作符以对存储在数组中的文档进行重新排序。 |
3. Bitwise
Name | Description |
---|---|
$bit | Performs bitwise AND , OR , and XOR updates of integer values. |