定义
Mongo.bulkWrite() 在一次调用中跨多个数据库和集合执行多个写入操作,这与对单个集合进行操作的db.collection.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
replaceOne
updateOne
updateMany
deleteOne
deleteMany
以下部分描述了表示每个操作的文档必须使用的语法。
insertOne
{ namespace: '<db.collection>', name: 'insertOne', document: Document }
字段 | 类型 | 说明 |
|---|---|---|
| 字符串 | 用于插入操作的数据库和集合。 |
| 字符串 | 操作名称。设置为 |
| 文档 | 要插入的文档。 |
注意
如果文档不包含 _id字段,mongosh 会自动生成一个字段。
更新一个和更新多个
updateOne 更新与过滤匹配的第一个文档。 updateMany 更新与过滤匹配的所有文档。
{ namespace: '<db>.<collection>', name: 'updateOne' | 'updateMany', filter: Document, update: Document | Document[], arrayFilters?: Document[], hint?: Document | string, collation?: Document, upsert?: boolean }
字段 | 类型 | 说明 |
|---|---|---|
| 字符串 | 用于更新操作的数据库和集合。 |
| 字符串 | 操作名称。设置为 |
| 文档 | 与要更新的一个或多个文档相匹配的过滤。 |
| 文档或文档大量 | 要执行的更新。 |
| 文档数组 | 可选。筛选器以指定在更新数组值字段时要更新的大量元素。 |
| 文档或字符串 | 可选。用于操作的索引。 |
| 文档 | (可选)对结果进行排序时使用的排序规则。 |
| 布尔 | 可选。如果为 |
replaceOne
{ namespace: '<db>.<collection>', name: 'replaceOne', filter: Document, replacement: Document, hint?: Document | string, collation?: Document }
字段 | 类型 | 说明 |
|---|---|---|
| 字符串 | 用于替换操作的数据库和集合。 |
| 字符串 | 操作名称。设置为 |
| 文档 | 与要更新的文档匹配的过滤。 |
| 文档 | 替换文档。 |
| 文档或字符串 | 可选。用于操作的索引。 |
| 文档 | (可选)对结果进行排序时使用的排序规则。 |
删除一个或多个
deleteOne 删除与过滤匹配的第一个文档。 deleteMany 删除与过滤匹配的所有文档。
{ namespace: '<db>.<collection>', name: 'deleteOne' | 'deleteMany', filter: Document, hint?: Document | string, collation?: Document }
字段 | 类型 | 说明 |
|---|---|---|
| 字符串 | 用于删除操作的数据库和集合。 |
| 字符串 | 操作名称。设置为 |
| 文档 | 匹配要删除的文档的查询选择器。 |
| 文档或字符串 | 可选。用于操作的索引。 |
| 文档 | 可选。用于操作的排序规则。 |
选项
{ 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) }
字段 | 类型 | 说明 |
|---|---|---|
| 布尔 |
|
| 整型 | 插入的文档数。 |
| 整型 | 过滤匹配的文档数。 |
| 整型 | 修改的文档数量。 |
| 整型 | 已删除的文档数量。 |
| 整型 | 更新或插入的文档数量。 |
| 整数到文档的映射 | 可选。表示每个成功插入操作的结果。每个操作都由一个整数键表示,其中包含具有与该操作相对应的信息的文档。文档包含以下字段:
|
| 整数到文档的映射 | 可选。表示每个成功更新操作的结果。每个操作都由一个整数键表示,其中包含具有与该操作相对应的信息的文档。文档包括以下字段:
|
| 整数到文档的映射 | 可选。表示每个成功删除操作的结果。每个操作都由一个整数键表示,其中包含具有与该操作相对应的信息的文档。文档包含以下字段:
|
示例
此示例使用 Mongo.bulkWrite() 按顺序执行以下操作:
将文档插入到
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 } } }