Docs 菜单
Docs 主页
/ /

Mongo.bulkWrite()(mongosh方法)

Mongo.bulkWrite(operations, options)

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

文档数组

定义写入操作大量。大量中的每个文档代表要执行的一个写入操作。

options

文档

operations 中的文档可以表示六种操作之一:

  • insertOne

  • replaceOne

  • updateOne

  • updateMany

  • deleteOne

  • deleteMany

以下部分描述了表示每个操作的文档必须使用的语法。

{
namespace: '<db.collection>',
name: 'insertOne',
document: Document
}
字段
类型
说明

namespace

字符串

用于插入操作的数据库和集合。

name

字符串

操作名称。设置为 "insertOne"

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
}
字段
类型
说明

namespace

字符串

用于更新操作的数据库和集合。

name

字符串

操作名称。设置为 "updateOne""updateMany"

filter

文档

与要更新的一个或多个文档相匹配的过滤。

update

文档或文档大量

要执行的更新。

arrayFilters

文档数组

可选。筛选器以指定在更新数组值字段时要更新的大量元素。

hint

文档或字符串

可选。用于操作的索引。

collation

文档

(可选)对结果进行排序时使用的排序规则

upsert

布尔

可选。如果为 true,则在未找到匹配项时创建一个文档。默认为 false

{
namespace: '<db>.<collection>',
name: 'replaceOne',
filter: Document,
replacement: Document,
hint?: Document | string,
collation?: Document
}
字段
类型
说明

namespace

字符串

用于替换操作的数据库和集合。

name

字符串

操作名称。设置为 "replaceOne"

filter

文档

与要更新的文档匹配的过滤。

replacement

文档

替换文档。

hint

文档或字符串

可选。用于操作的索引。

collation

文档

(可选)对结果进行排序时使用的排序规则

deleteOne 删除与过滤匹配的第一个文档。 deleteMany 删除与过滤匹配的所有文档。

{
namespace: '<db>.<collection>',
name: 'deleteOne' | 'deleteMany',
filter: Document,
hint?: Document | string,
collation?: Document
}
字段
类型
说明

namespace

字符串

用于删除操作的数据库和集合。

name

字符串

操作名称。设置为 "deleteOne""deleteMany"

filter

文档

匹配要删除的文档的查询选择器。

hint

文档或字符串

可选。用于操作的索引。

collation

文档

可选。用于操作的排序规则。

{
ordered?: boolean,
verboseResults?: boolean,
bypassDocumentValidation?: boolean,
let?: Document
}
字段
类型
说明

ordered

布尔

(可选)表示MongoDB按照您提供的文档顺序执行批量写入。如果为 true,则在出现第一个错误时停止。如果为 false,则即使某些操作失败,也会继续处理剩余操作。默认为 true

verboseResults

布尔

(可选)指定 bulkWrite() 是否输出详细结果。默认为 false

bypassDocumentValidation

布尔

(可选)指定写入操作是否绕过文档验证规则。默认为 false

let

文档

(可选)可使用聚合变量访问权限的参数名称和值的文档。

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)
}
字段
类型
说明

acknowledged

布尔

true 如果服务器返回确认,则返回 false

insertedCount

整型

插入的文档数。

matchedCount

整型

过滤匹配的文档数。

modifiedCount

整型

修改的文档数量。

deletedCount

整型

已删除的文档数量。

upsertedCount

整型

更新或插入的文档数量。

insertResults

整数到文档的映射

可选。表示每个成功插入操作的结果。每个操作都由一个整数键表示,其中包含具有与该操作相对应的信息的文档。文档包含以下字段:

  • insertedId:ObjectId。 表示插入文档的 _id

updateResults

整数到文档的映射

可选。表示每个成功更新操作的结果。每个操作都由一个整数键表示,其中包含具有与该操作相对应的信息的文档。文档包括以下字段:

  • matchedCount:整数。表示匹配的文档数。

  • modifiedCount:整数。表示修改的文档数。

  • upsertedId:ObjectId。 表示任何已更新或插入的文档的 _id。可选。

  • didUpsert:布尔值。如果已更新或插入文档,则为 true,否则为 false

deleteResults

整数到文档的映射

可选。表示每个成功删除操作的结果。每个操作都由一个整数键表示,其中包含具有与该操作相对应的信息的文档。文档包含以下字段:

  • deletedCount:整数。表示已删除的文档数。

此示例使用 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 } }
}

后退

db.集合.initializeUnorderedBulkOp

在此页面上