批量操作
在此页面上
Overview
在本指南中,您可以学习;了解如何使用 Node.js驾驶员执行批量操作。批量操作有助于减少对服务器的调用次数。您可以在一个动作中执行多个操作,而无需为每个操作发送请求。
您可以使用批量操作对集合执行多个写入操作。您还可以从客户端运行批量操作,这样就可以跨多个命名空间执行批量写入。在MongoDB中,命名空间由数据库名称和集合名称组成,格式为 <database>.<collection>
。
本指南包括以下部分:
其他信息提供了指向本指南中提及的类型和方法的资源和API文档的链接。
重要
服务器和驱动程序版本要求
集合级批量写入操作需要以下版本:
MongoDB Server 3.2 或更高版本
Node.js驾驶员版本 3.6 或更高版本
客户端级批量写入操作需要以下版本:
MongoDB Server 8.0 或更高版本
Node.js驾驶员版本 6.10 或更高版本
样本数据
本指南中的示例使用movies
users
sample_mflix
数据库中的 和 集合,该数据库包含在Atlas示例数据集中。要学习;了解如何创建免费的MongoDB Atlas 群集并加载示例数据集,请参阅Atlas入门指南。
批量插入操作
要执行批量插入操作,请为要插入的每个文档创建批量操作模型。然后,将这些模型的列表传递给 bulkWrite()
方法。
本节介绍如何执行以下类型的批量操作:
集合批量插入
要对集合执行批量插入操作,请为每个操作创建一个 InsertOneModel
。然后,对集合调用 bulkWrite()
方法,并将模型大量作为参数传递。要创建 InsertOneModel
,请指定模型的 document
字段并将其设立为要插入的文档。
例子
此示例将执行以下动作:
在大量中指定两个
InsertOneModel
实例。每个InsertOneModel
代表一个要插入到sample_mflix
数据库的movies
集合中的文档。在
movies
集合上调用bulkWrite()
方法,并将模型大量作为参数传递。打印插入文档的数量。
const insertModels = [{ insertOne: { document: { title: "The Favourite", year: 2018, rated: "R", released: "2018-12-21" } } }, { insertOne: { document: { title: "I, Tonya", year: 2017, rated: "R", released: "2017-12-08" } } }]; const insertResult = await movies.bulkWrite(insertModels); console.log(`Inserted documents: ${insertResult.insertedCount}`);
Inserted documents: 2
客户端批量插入
要跨多个集合或数据库执行批量插入操作,请为每个操作创建一个 ClientBulkWriteModel
。然后,在客户端上调用 bulkWrite()
方法并将模型大量作为参数传递。
下表描述了您可以在 ClientBulkWriteModel
中设立以指定插入操作的字段:
字段 | 说明 |
---|---|
| The namespace in which to insert a document. Type: String |
| The operation you want to perform. For insert operations,
set this field to "insertOne" .Type: String |
| The document to insert. Type: Document |
例子
此示例将执行以下动作:
在一个大量中指定三个
ClientBulkWriteModel
实例。前两个模型表示要插入到movies
集合中的文档,最后一个模型表示要插入到users
集合中的文档。在客户端上调用
bulkWrite()
方法,并将模型大量作为参数传递。打印插入文档的数量。
const clientInserts = [{ namespace: "sample_mflix.movies", name: "insertOne", document: { title: "The Favourite", year: 2018, rated: "R", released: "2018-12-21" } }, { namespace: "sample_mflix.movies", name: "insertOne", document: { title: "I, Tonya", year: 2017, rated: "R", released: "2017-12-08" } }, { namespace: "sample_mflix.users", name: "insertOne", document: { name: "Brian Schwartz", email: "bschwartz@example.com" } }]; const clientInsertRes = await client.bulkWrite(clientInserts); console.log(`Inserted documents: ${clientInsertRes.insertedCount}`);
Inserted documents: 3
批量替换操作
要执行批量替换操作,请为要替换的每个文档创建批量操作模型。然后,将这些模型的列表传递给 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 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()
方法,并将模型大量作为参数传递。打印已修改文档的数量。
const replaceOperations = [{ replaceOne: { filter: { title: "The Dark Knight" }, replacement: { title: "The Dark Knight Rises", year: 2012, rating: "PG-13" }, upsert: false } }, { replaceOne: { filter: { title: "Inception" }, replacement: { title: "Inception Reloaded", year: 2010, rating: "PG-13" }, upsert: false } }]; const replaceResult = await movies.bulkWrite(replaceOperations); console.log(`Modified documents: ${replaceResult.modifiedCount}`);
Modified documents: 2
客户端批量替换
要跨多个集合或数据库执行批量替换操作,请为每个操作创建一个 ClientBulkWriteModel
。然后,在客户端上调用 bulkWrite()
方法并将模型大量作为参数传递。
下表描述了您可以在 ClientBulkWriteModel
中设立以指定替换操作的字段:
字段 | 说明 |
---|---|
| The namespace in which to replace a document. Type: String |
| The operation you want to perform. For replace operations,
set this field to "replaceOne" .Type: String |
| 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 guide. Type: Bson |
例子
此示例将执行以下动作:
在一个大量中指定三个
ClientBulkWriteModel
实例。前两个模型包含movies
集合中文档的替换指令,最后一个模型包含users
集合中文档的替换指令。在客户端上调用
bulkWrite()
方法,并将模型大量作为参数传递。打印已修改文档的数量。
const clientReplacements = [{ namespace: "sample_mflix.movies", name: "replaceOne", filter: { title: "The Dark Knight" }, replacement: { title: "The Dark Knight Rises", year: 2012, rating: "PG-13" } }, { namespace: "sample_mflix.movies", name: "replaceOne", filter: { title: "Inception" }, replacement: { title: "Inception Reloaded", year: 2010, rating: "PG-13" } }, { namespace: "sample_mflix.users", name: "replaceOne", filter: { name: "April Cole" }, replacement: { name: "April Franklin", email: "aprilfrank@example.com" } }]; const clientReplaceRes = await client.bulkWrite(clientReplacements); console.log(`Modified documents: ${clientReplaceRes.modifiedCount}`);
Modified documents: 3
批量更新操作
要执行批量更新操作,请为要进行的每个更新创建批量操作模型。然后,将这些模型的列表传递给 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 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()
方法,并将模型大量作为参数传递。打印已修改文档的数量。
const updateOperations = [{ updateOne: { filter: { title: "Interstellar" }, update: { $set: { title: "Interstellar Updated", genre: "Sci-Fi Adventure" } }, upsert: true } }, { updateMany: { filter: { rated: "PG-13" }, update: { $set: { rated: "PG-13 Updated", genre: "Updated Genre" } } } }]; const updateResult = await movies.bulkWrite(updateOperations); console.log(`Modified documents: ${updateResult.modifiedCount}`);
Modified documents: 2320
客户端批量更新
要跨多个集合或数据库执行批量更新操作,请为每个操作创建一个 ClientBulkWriteModel
。然后,在客户端上调用 bulkWrite()
方法并将模型大量作为参数传递。
下表描述了您可以在 ClientBulkWriteModel
中设立以指定更新操作的字段:
字段 | 说明 |
---|---|
| The namespace in which to update a document. Type: String |
| The operation you want to perform. For update operations,
set this field to "updateOne" or "updateMany" .Type: String |
| The filter that matches one or more documents you want to update. If
you set the model name to "updateOne" , only the first matching
document is updated. If you set name to "updateMany" , all
matching documents are updated.Type: Document |
| The updates to perform. Type: Document or Document[] |
| (Optional) A set of filters specifying which array elements an update
applies to if you are updating an array-valued field. Type: Document[] |
| (Optional) The collation to use when sorting results. To learn more about
collations, see the Collations guide. Type: Document |
| (Optional) The index to use for the operation. To learn more about
indexes, see the Indexes guide. Type: Document or String |
| (Optional) Whether a new document is created if no document matches the filter.
By default, this field is set to false .Type: Boolean |
例子
此示例将执行以下动作:
在大量中指定两个
ClientBulkWriteModel
实例。第一个模型指定对movies
集合进行多次更新操作,第二个模型指定对users
集合进行一次更新操作。在客户端上调用
bulkWrite()
方法,并将模型大量作为参数传递。打印已修改文档的数量。
const clientUpdates = [{ namespace: "sample_mflix.movies", name: "updateMany", filter: { rated: "PG-13" }, update: { $set: { rated: "PG-13 Updated", genre: "Updated Genre" } }, upsert: false }, { namespace: "sample_mflix.users", name: "updateOne", filter: { name: "Jon Snow" }, update: { $set: { name: "Aegon Targaryen", email: "targaryen@example.com" } }, upsert: false }]; const clientUpdateRes = await client.bulkWrite(clientUpdates); console.log(`Modified documents: ${clientUpdateRes.modifiedCount}`);
Modified documents: 2320
批量删除操作
要执行批量删除操作,请为每个删除操作创建批量操作模型。然后,将这些模型的列表传递给 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 guide. Type: String or Object |
例子
此示例将执行以下动作:
在大量中指定一个
DeleteOneModel
和一个DeleteManyModel
实例。这些模型包含删除movies
集合中文档的指令。在
movies
集合上调用bulkWrite()
方法,并将模型大量作为参数传递。打印已删除文档的数量。
const deleteOperations = [{ deleteOne: { filter: { title: "Dunkirk" } } }, { deleteMany: { filter: { rated: "R" } } }]; const deleteResult = await movies.bulkWrite(deleteOperations); console.log(`Deleted documents: ${deleteResult.deletedCount}`);
Deleted documents: 5538
客户端批量删除
要跨多个集合或数据库执行批量删除操作,请为每个操作创建一个 ClientBulkWriteModel
。然后,在客户端上调用 bulkWrite()
方法并将模型大量作为参数传递。
下表描述了您可以在 ClientBulkWriteModel
中设立以指定删除操作的字段:
字段 | 说明 |
---|---|
| The namespace in which to delete a document. Type: String |
| The operation you want to perform. For delete operations,
set this field to "deleteOne" or "deleteMany" .Type: String |
| The filter that matches one or more documents you want to delete. If
you set the model name to "deleteOne" , only the first matching
document is deleted. If you set name to "deleteMany" , all
matching documents are deleted.Type: Document |
| (Optional) The index to use for the operation. To learn more about
indexes, see the Indexes guide. Type: Document or String |
| (Optional) The collation to use when sorting results. To learn more about
collations, see the Collations guide. Type: Document |
例子
此示例将执行以下动作:
在大量中指定两个
ClientBulkWriteModel
实例。第一个模型指定对movies
集合执行“删除多个”操作,第二个模型指定对users
集合执行“删除一个”操作。在客户端上调用
bulkWrite()
方法,并将模型大量作为参数传递。打印已修改文档的数量。
const clientDeletes = [{ namespace: "sample_mflix.movies", name: "deleteMany", filter: { rated: "R" } }, { namespace: "sample_mflix.users", name: "deleteOne", filter: { email: "emilia_clarke@gameofthron.es" } }]; const clientDeleteRes = await client.bulkWrite(clientDeletes); console.log(`Deleted documents: ${clientDeleteRes.deletedCount}`);
Deleted documents: 5538
返回类型
BulkWriteResult
Collection.bulkWrite()
方法返回一个 BulkWriteResult
对象,其中提供有关批量操作的信息。
下表描述了 BulkWriteResult
对象的字段:
字段 | 说明 |
---|---|
| 插入文档的数量 |
| 匹配文档的数量 |
| 更新文档的数量 |
| 已更新或插入的文档数量 |
| 已删除文档的数量 |
ClientBulkWriteResult
MongoClient.bulkWrite()
方法返回一个 ClientBulkWriteResult
对象,其中包含有关客户端端批量写入操作的信息。
下表描述了 ClientBulkWriteResult
对象的字段:
字段 | 说明 |
---|---|
| 指示批量写入是否已确认的布尔值 |
| 插入文档的数量 |
| 匹配文档的数量 |
| 更新文档的数量 |
| 已更新或插入的文档数量 |
| 已删除文档的数量 |
| 每个成功插入操作的结果 |
| 每个成功更新操作的结果 |
| 每个成功删除操作的结果 |
处理异常
集合批量写入异常
如果对集合调用的任何批量写入操作不成功,则当 ordered
选项设立为 true
时,Node.js驾驶员会抛出 MongoBulkWriteError
,并且不会执行任何进一步的操作。如果将 ordered
设立false
,则会尝试继续后续操作。
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 |
客户端批量写入异常
如果在客户端上调用的任何批量写入操作不成功,Node.js驾驶员都会生成一个 MongoClientBulkWriteError
。默认下,驾驶员在出现错误后不执行任何后续操作。如果将 ordered
选项传递给 bulkWrite()
方法并将其设立为 false
,驾驶员将继续尝试其余操作。
MongoClientBulkWriteError
对象包含以下属性:
属性 | 说明 |
---|---|
| An array of documents specifying each write concern error. Type: Document[] |
| An map of errors that occurred during individual write operations. Type: Map<number, ClientBulkWriteError> |
| The partial result of the client bulk write that reflects the operation's
progress before the error. Type: ClientBulkWriteResult |
更多信息
要学习;了解有关批量操作的更多信息,请参阅MongoDB Server手册中的批量写入操作。
API 文档
要进一步了解本指南所讨论的任何方法或类型,请参阅以下 API 文档: