# 更新运算符

在本页面

* [更新运算符](#update-operators)

以下修饰符可用于更新操作；例如在[`db.collection.update()`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators)和中 [`db.collection.findAndModify()`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators)。

在以下格式的文档中指定运算符表达式：

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

> **注意**
>
> 有关特定运算符的详细信息，包括语法和示例，请单击特定运算符以转到其参考页。

## 更新运算符

### 字段

| 名称                                                                               | 描述                                   |
| -------------------------------------------------------------------------------- | ------------------------------------ |
| [`$currentDate`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators) | 将字段的值设置为当前日期，即日期或时间戳。                |
| [`$inc`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators)         | 将字段的值增加指定的数量。                        |
| [`$min`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators)         | 仅当指定值小于现有字段值时才更新该字段。                 |
| [`$max`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators)         | 仅当指定值大于现有字段值时才更新该字段。                 |
| [`$mul`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators)         | 将字段的值乘以指定的数量。                        |
| [`$rename`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators)      | 重命名字段。                               |
| [`$set`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators)         | 设置文档中字段的值。                           |
| [`$setOnInsert`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators) | 如果更新导致插入文档，则设置字段的值。对修改现有文档的更新操作没有影响。 |
| [`$unset`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators)       | 从文档中删除指定的字段。                         |

### 数组

#### 运算符

| 名称                                                                                  | 描述                                            |
| ----------------------------------------------------------------------------------- | --------------------------------------------- |
| [`$`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators)               | 充当占位符，以更新与查询条件匹配的第一个元素。                       |
| [`$[]`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators)             | 充当占位符，以更新匹配查询条件的文档的数组中的所有元素。                  |
| [`$[<identifier>]`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators) | 充当占位符，以更新`arrayFilters`与查询条件匹配的文档中所有与条件匹配的元素。 |
| [`$addToSet`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators)       | 仅当元素不存在于集合中时才将它们添加到数组中。                       |
| [`$pop`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators)            | 删除数组的第一项或最后一项。                                |
| [`$pull`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators)           | 删除与指定查询匹配的所有数组元素。                             |
| [`$push`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators)           | 将项目添加到数组。                                     |
| [`$pullAll`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators)        | 从数组中删除所有匹配的值。                                 |

#### 修饰符

| 名称                                                                            | 描述                                     |
| ----------------------------------------------------------------------------- | -------------------------------------- |
| [`$each`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators)     | 修改`$push`和`$addToSet`运算符以附加多个项以进行数组更新。 |
| [`$position`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators) | 修改`$push`运算符以指定要添加元素的数组中的位置。           |
| [`$slice`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators)    | 修改`$push`运算符以限制更新数组的大小。                |
| [`$sort`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators)     | 修改`$push`运算符以对存储在数组中的文档重新排序。           |

### 按位

| 名称                                                                       | 描述                          |
| ------------------------------------------------------------------------ | --------------------------- |
| [`$bit`](https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators) | 执行按位`AND`，`OR`和`XOR`整数值的更新。 |

译者：李冠飞

校对：


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mongoing.com/can-kao/yun-suan-fu/update-operators.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
