Overview
在本指南中,您可以了解如何使用 Node.js 驱动程序执行批量操作。批量操作有助于减少对服务器的调用次数。您可以在一次执行多个操作,而不是为每个操作发送请求。
提示
要学习;了解有关批量操作的更多信息,请参阅MongoDB Server手册中的批量写入操作。
本指南包括以下部分:
批量插入操作描述了如何在集合或客户端上执行批量插入操作。
批量替换操作描述了如何在集合或客户端上执行批量替换操作。
批量更新操作描述了如何在集合或客户端上执行批量更新操作。
批量删除操作描述了如何在集合或客户端上执行批量删除操作。
其他信息提供了指向本指南中提及的类型和方法的资源和API文档的链接。
重要
服务器和驱动程序版本要求
集合级批量写入操作需要以下版本:
MongoDB Server 3.2 或更高版本
Node.js驱动程序版本 3.6 或更高版本
样本数据
本指南中的示例使用movies userssample_mflix数据库中的 和 集合,该数据库包含在Atlas示例数据集中。要学习;了解如何创建免费的MongoDB Atlas 群集并加载示例数据集,请参阅Atlas入门指南。
批量插入操作
要执行批量插入操作,请为要插入的每个文档创建批量操作模型。然后,将这些模型的列表传递给 bulkWrite() 方法。
集合批量插入
要对集合执行批量插入操作,请为每个操作创建一个 InsertOneModel。然后,对集合调用 bulkWrite() 方法,并将模型大量作为参数传递。要创建 InsertOneModel,请指定模型的 document字段并将其设立为要插入的文档。
例子
此示例将执行以下动作:
在大量中指定两个
InsertOneModel实例。每个InsertOneModel代表一个要插入到sample_mflix数据库的movies集合中的文档。在
movies集合上调用bulkWrite()方法,并将模型大量作为参数传递。打印插入文档的数量。
批量替换操作
要执行批量替换操作,请为要替换的每个文档创建批量操作模型。然后,将这些模型的列表传递给 bulkWrite() 方法。
集合批量替换
要对集合执行批量替换操作,请为每个操作创建一个 ReplaceOneModel。然后,对集合调用 bulkWrite() 方法,并将模型大量作为参数传递。
下表描述了您可以在 ReplaceOneModel 中设立的字段:
字段 | 说明 |
|---|---|
| The filter that matches the document you want to replace. Type: Document |
| The replacement document. Type: Document |
| (Optional) The collation to use when sorting results. To learn more
about collations, see the Collations guide. Type: String or Object |
| (Optional) The index to use for the operation. To learn more about
indexes, see the Indexes on Collections guide. Type: Bson |
| (Optional) Whether a new document is created if no document matches the filter. By default, this field is set to false.Type: Boolean |
例子
此示例将执行以下动作:
在大量中指定两个
ReplaceOneModel实例。ReplaceOneModel实例包含替换movies集合中表示电影的文档的指令。在
movies集合上调用bulkWrite()方法,并将模型大量作为参数传递。打印已修改文档的数量。
批量更新操作
要执行批量更新操作,请为要进行的每个更新创建批量操作模型。然后,将这些模型的列表传递给 bulkWrite() 方法。
集合批量更新
要对集合执行批量更新操作,请为每个操作创建一个 UpdateOneModel 或 UpdateManyModel。然后,对集合调用 bulkWrite() 方法,并将模型大量作为参数传递。UpdateOneModel 仅更新与过滤匹配的一个文档,而 UpdateManyModel 会更新与过滤匹配的所有文档。
下表描述了您可以在 UpdateOneModel 或 UpdateManyModel 中设立的字段:
字段 | 说明 |
|---|---|
| The filter that matches one or more documents you want to update. When
specified in an UpdateOneModel, only the first matching document will
be updated. When specified in an UpdateManyModel, all matching documents
will be updated.Type: Document |
| The update to perform. Type: Document |
| (Optional) A set of filters specifying which array elements an update
applies to if you are updating an array-valued field. Type: Array |
| (Optional) The collation to use when sorting results. To learn more about
collations, see the Collations guide. Type: Object |
| (Optional) The index to use for the operation. To learn more about
indexes, see the Indexes on Collections guide. Type: String or Object |
| (Optional) Whether a new document is created if no document matches the filter.
By default, this field is set to false.Type: Boolean |
例子
此示例将执行以下动作:
在大量中指定一个
UpdateOneModel和一个UpdateManyModel实例。 这些模型包含更新表示movies集合中的电影的文档的指令。在
movies集合上调用bulkWrite()方法,并将模型大量作为参数传递。打印已修改文档的数量。
批量删除操作
要执行批量删除操作,请为每个删除操作创建批量操作模型。然后,将这些模型的列表传递给 bulkWrite() 方法。
集合批量删除
要对集合执行批量删除操作,请为每个操作创建一个 DeleteOneModel 或 DeleteManyModel。然后,对集合调用 bulkWrite() 方法,并将模型大量作为参数传递。DeleteOneModel 仅删除与过滤匹配的一个文档,而 DeleteManyModel 会删除与过滤匹配的所有文档。
下表描述了可以在 DeleteOneModel 或 DeleteManyModel 中设立的字段:
字段 | 说明 |
|---|---|
| The filter that matches one or more documents you want to delete. When
specified in a DeleteOneModel, only the first matching document will
be deleted. When specified in a DeleteManyModel, all matching documents
will be deleted.Type: Document |
| (Optional) The collation to use when sorting results. To learn more about
collations, see the Collations guide. Type: Object |
| (Optional) The index to use for the operation. To learn more about
indexes, see the Indexes on Collections guide. Type: String or Object |
例子
此示例将执行以下动作:
在大量中指定一个
DeleteOneModel和一个DeleteManyModel实例。这些模型包含删除movies集合中文档的指令。在
movies集合上调用bulkWrite()方法,并将模型大量作为参数传递。打印已删除文档的数量。
返回类型
Collection.bulkWrite() 方法返回一个 BulkWriteResult对象,其中提供有关批量操作的信息。
下表描述了 BulkWriteResult对象的字段:
字段 | 说明 |
|---|---|
| 插入文档的数量 |
| 匹配文档的数量 |
| 更新文档的数量 |
| 已更新或插入的文档数量 |
| 已删除文档的数量 |
处理异常
如果对集合调用的任何批量写入操作不成功,则当 ordered 选项设立为 true 时,Node.js驱动程序会抛出 MongoBulkWriteError,并且不会执行任何进一步的操作。如果将 ordered设立false,则会尝试继续后续操作。
提示
要学习;了解有关有序和无序批量操作的更多信息,请参阅MongoDB Server手册中批量写入指南中的有序与无序操作部分。
MongoBulkWriteError对象包含以下属性:
属性 | 说明 |
|---|---|
| The error message. Type: String |
| An array of errors that occurred during the bulk write operation. Type: BulkWriteError[] |
| Write concern errors that occurred during execution of the bulk write operation. Type: WriteConnectionError[] |
| The results of any successful operations performed before the exception was
thrown. Type: BulkWriteResult[] |
| The underlying error object, which may contain more details. Type: Error |
更多信息
要学习;了解有关批量操作的更多信息,请参阅MongoDB Server手册中的批量写入操作。
API 文档
要进一步了解本指南所讨论的任何方法或类型,请参阅以下 API 文档: