# MongoDB CRUD操作

**在本页面**

* [创建操作](#创建)
* [读取操作](#读取)
* [更新操作](#更新)
* [删除操作](#删除)
* [批量写入](#批量)

  **CURD操作指的是文档的*****创建*****、*****读*****、*****更新*****以及*****删除*****操作。**

## 创建操作

创建或插入操作会将新[文档](https://docs.mongodb.com/master/core/document/#bson-document-format)添加到[集合](https://docs.mongodb.com/master/core/databases-and-collections/#collections)中。 如果该集合当前不存在，则插入操作将创建该集合。

MongoDB提供以下将文档插入集合的方法：

* [`db.collection.insertOne()`](https://docs.mongodb.com/manual/reference/method/db.collection.insertOne/#db.collection.insertOne) *3.2版中的新功能*
* [`db.collection.insertMany()`](https://docs.mongodb.com/manual/reference/method/db.collection.insertMany/#db.collection.insertMany) *3.2版中的新功能*

在MongoDB中，插入操作针对单个[集合](https://docs.mongodb.com/master/core/databases-and-collections/#collections)。 MongoDB中的所有写操作都是单个文档级别的[原子](https://docs.mongodb.com/master/core/write-operations-atomicity/)操作。![](https://docs.mongodb.com/master/_images/crud-annotated-mongodb-insertOne.bakedsvg.svg)

有关示例，请参见[插入文档](https://docs.mongodb.com/manual/tutorial/insert-documents/)。

## 读取操作

读取操作从集合中检索文档； 即查询集合中的文档。 MongoDB提供了以下方法来从集合中读取文档：

* [db.collection.find()](https://docs.mongodb.com/manual/reference/method/db.collection.find/#db.collection.find)

您可以指定查询过滤器或条件以标识要返回的文档。

![](https://docs.mongodb.com/master/_images/crud-annotated-mongodb-find.bakedsvg.svg)

**有关示例，请参见：**

* [查询文件](https://docs.mongodb.com/manual/tutorial/query-documents/)
* [查询嵌入/嵌套文档](https://docs.mongodb.com/manual/tutorial/query-embedded-documents/)
* [查询数组](https://docs.mongodb.com/manual/tutorial/query-arrays/)
* [查询嵌入式文档数组](https://docs.mongodb.com/manual/tutorial/query-array-of-documents/)

## 更新操作

更新操作会修改集合中的现有文档。 MongoDB提供了以下更新集合文档的方法：

* [`db.collection.updateOne()`](https://docs.mongodb.com/manual/reference/method/db.collection.updateOne/#db.collection.updateOne) *3.2版中的新功能*
* [`db.collection.updateMany()`](https://docs.mongodb.com/manual/reference/method/db.collection.updateMany/#db.collection.updateMany) *3.2版中的新功能*
* [`db.collection.replaceOne()`](https://docs.mongodb.com/manual/reference/method/db.collection.replaceOne/#db.collection.replaceOne) *3.2版中的新功能*

在MongoDB中，更新操作针对单个集合。 MongoDB中的所有写操作都是单个文档级别的原子操作。

您可以指定标准或过滤器，以标识要更新的文档。 这些过滤器使用与读取操作相同的语法。![](https://docs.mongodb.com/master/_images/crud-annotated-mongodb-updateMany.bakedsvg.svg)

有关示例，请参见[更新文档](https://docs.mongodb.com/manual/tutorial/update-documents/)。

## 删除操作

删除操作从集合中删除文档。 MongoDB提供以下删除集合文档的方法：

* [`db.collection.deleteOne()`](https://docs.mongodb.com/manual/reference/method/db.collection.deleteOne/#db.collection.deleteOne) *3.2版中的新功能*
* [`db.collection.deleteMany()`](https://docs.mongodb.com/manual/reference/method/db.collection.deleteMany/#db.collection.deleteMany) *3.2版中的新功能*

在MongoDB中，删除操作只针对单个集合。MongoDB中的所有写操作都是单个文档级别的原子 操作。

你可以指定查询过滤器或条件来标识要更新的文档，这里的过滤器和读操作的语法是一致的。

![](https://docs.mongodb.com/master/_images/crud-annotated-mongodb-deleteMany.bakedsvg.svg)

有关示例，请参见[删除文档](https://docs.mongodb.com/manual/tutorial/remove-documents/)。

## 批量写入

MongoDB提供了批量执行写入操作的功能。有关详细信息，请参见[批量写入操作](https://docs.mongodb.com/manual/core/bulk-write-operations/)。

译者：刘翔 杨帅

### MongoDB中文社区

![MongoDB中文社区—MongoDB爱好者技术交流平台](https://mongoing.com/wp-content/uploads/2020/09/6de8a4680ef684d-2.png)

| 资源列表推荐             | 资源入口                                                                                                                                                                                                                                                                       |
| ------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| MongoDB中文社区官网      | <https://mongoing.com/>                                                                                                                                                                                                                                                    |
| 微信服务号 ——最新资讯和优质文章  | Mongoing中文社区（mongoing-mongoing）                                                                                                                                                                                                                                            |
| 微信订阅号 ——发布文档翻译内容   | MongoDB中文用户组（mongoing123）                                                                                                                                                                                                                                                  |
| 官方微信号 —— 官方最新资讯    | MongoDB数据库（MongoDB-China）                                                                                                                                                                                                                                                  |
| MongoDB中文社区组委会成员介绍 | <https://mongoing.com/core-team-members>                                                                                                                                                                                                                                   |
| MongoDB中文社区翻译小组介绍  | <https://mongoing.com/translators>                                                                                                                                                                                                                                         |
| MongoDB中文社区微信技术交流群 | 添加社区助理小芒果微信（ID:mongoingcom），并备注 mongo                                                                                                                                                                                                                                      |
| MongoDB中文社区会议及文档资源 | <https://mongoing.com/resources>                                                                                                                                                                                                                                           |
| MongoDB中文社区大咖博客    | [基础知识](https://mongoing.com/basic-knowledge) [性能优化](https://mongoing.com/performance-optimization) [原理解读](https://mongoing.com/interpretation-of-principles) [运维监控](https://mongoing.com/operation-and-maintenance-monitoring) [最佳实践](https://mongoing.com/best-practices) |
| MongoDB白皮书         | <https://mongoing.com/mongodb-download-white-paper>                                                                                                                                                                                                                        |
| MongoDB初学者教程-7天入门  | <https://mongoing.com/mongodb-beginner-tutorial>                                                                                                                                                                                                                           |
| 社区活动邮件订阅           | <https://sourl.cn/spszjN>                                                                                                                                                                                                                                                  |


---

# 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/mongodb-crud-operations.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.
