Mongo.bulkWrite()
定义
bulkWrite()
在一次调用中跨多个数据库和集合执行多个写入操作。此方法会替换 db.collection.bulkWrite()
,后者在一次调用中对特定集合执行多个写入操作。
注意
您只能将 Mongo.bulkWrite()
与MongoDB 8.0 或更高版本一起使用。
语法
您可以使用以下语法在当前Mongo()
实例上调用 bulkWrite()
:
db.getMongo().bulkWrite( [ { namespace: "<db1.collection1>", name: "insertOne", document: { ... } }, { namespace: "<db2.collection2>", name: "replaceOne", filter: { ... } } ], { ordered: boolean, verboseResults: boolean, bypassDocumentValidation: boolean, let: Document } )
您也可以在不同的 Mongo
实例上调用它,如以下示例所示:
const otherMongo = Mongo("<other connection string>"); otherMongo.bulkWrite([{ namespace: "<db.collection>", ... }]);
bulkWrite()
接受两个参数:
Parameter | 类型 | 说明 |
---|---|---|
| 文档数组 | 定义写入操作大量。大量中的每个文档代表要执行的一个写入操作。 |
| 文档 | 定义操作的选项。 |
operations
中的文档可以表示六种操作之一:
insertOne
替换一个
updateOne
updateMany
deleteOne
deleteMany
以下部分描述了表示每个操作的文档必须使用的语法。
insertOne
{ namespace: '<db.collection>', name: 'insertOne', document: Document }
字段 | 类型 | 说明 |
---|---|---|
| 字符串 | 要插入文档的数据库和集合。 |
| 字符串 | 操作。对于插入一个操作,设置为 |
| 文档 | 要插入的文档。 |
注意
如果未在要插入的文档中指定 _id
字段,mongosh
会自动生成 _id
。
更新一个或多个
{ namespace: '<db>.<collection>', name: 'updateOne' | 'updateMany', filter: Document, update: Document | Document[], arrayFilters?: Document[], hint?: Document | string, collation?: Document, upsert?: boolean }
字段 | 类型 | 说明 |
---|---|---|
| 字符串 | 要在其中更新文档的数据库和集合。 |
| 字符串 | 操作。对于更新一个操作,设置为 |
| 文档 | 与要更新的一个或多个文档相匹配的过滤。在更新一个操作期间, |
| 文档 | 要执行的更新。 |
| 文档数组 | (可选)如果更新数组值字段,则 |
| 文档或字符串 | (可选)用于操作的索引。 |
| 文档 | (可选)对结果进行排序时使用的排序规则。 |
| 布尔 | 指定在没有文档与过滤匹配的情况下MongoDB是否创建新文档。默认为 |
replaceOne
{ namespace: '<db>.<collection>', name: 'replaceOne', filter: Document, replacement: Document, hint?: Document | string, collation?: Document }
删除一个或多个
{ namespace: '<db>.<collection>', name: 'deleteOne' | 'deleteMany', filter: Document, hint?: Document | string, collation?: Document }
选项
您可以将以下选项与 bulkWrite()
一起使用。您可以将包含要使用的选项的文档传递给 bulkWrite()
。本文档为可选项。
{ ordered?: boolean, verboseResults?: boolean, bypassDocumentValidation?: boolean, let?: Document }
字段 | 类型 | 说明 |
---|---|---|
| 布尔 | (可选)表示MongoDB按照您提供的文档顺序执行批量写入。默认为 |
| 布尔 | (可选)指定 |
| 布尔 | (可选)指定写入操作是否绕过文档验证规则。默认为 |
| 文档 | (可选)可使用聚合变量访问权限的参数名称和值的文档。 |
输出
bulkWrite()
返回具有以下字段的对象:
{ acknowledged: boolean, insertedCount: int, matchedCount: int, modifiedCount: int, deletedCount: int, upsertedCount: int, insertResults?: map(int, document), updateResults?: map(int, document), deleteResults?: map(int, document) }
字段 | 类型 | 说明 |
---|---|---|
| 布尔 |
|
| 整型 | 插入的文档数。 |
| 整型 | 过滤匹配的文档数。 |
| 整型 | 修改的文档数量。 |
| 整型 | 已删除的文档数量。 |
| 整型 | 更新或插入的文档数量。 |
| 整数到文档的映射 | 可选。表示每个成功插入操作的结果。每个操作都由一个整数键表示,其中包含具有与该操作相对应的信息的文档。文档包含以下字段:
|
| 整数到文档的映射 | 可选。表示每个成功更新操作的结果。每个操作都由一个整数键表示,其中包含具有与该操作相对应的信息的文档。文档包括以下字段:
|
| 整数到文档的映射 | 可选。表示每个成功删除操作的结果。每个操作都由一个整数键表示,其中包含具有与该操作相对应的信息的文档。文档包含以下字段:
|
示例
此 mongosh
命令按顺序执行以下操作:
将文档插入到
db.authors
集合中将文档插入到
db.books
集合中更新上一个文档
db.getMongo().bulkWrite( [ { namespace: 'db.authors', name: 'insertOne', document: { name: 'Stephen King' } }, { namespace: 'db.books', name: 'insertOne', document: { name: 'It' } }, { namespace: 'db.books', name: 'updateOne', filter: { name: 'it' }, update: { $set: { year: 1986 } } } ], { ordered: true, bypassDocumentValidation: true } )
mongosh
按顺序执行批量写入并返回以下文档:
{ acknowledged: true, insertedCount: 2, matchedCount: 1, modifiedCount: 1, deletedCount: 0, upsertedCount: 0, insertResults: { '1': { insertedId: ObjectId('67ed8ce8efd926c84cab7945') }, '2': { insertedId: ObjectId('67ed8ce8efd926c84cab7946') } } updateResults: { '1': { matchedCount: 1, modifiedCount: 1, didUpsert: false } } }