Definition定义
Mongo.bulkWrite(operations, options)
bulkWrite() performs multiple write operations across multiple databases and collections in a single call. 在单个调用中跨多个数据库和集合执行多个写入操作。This method replaces 此方法替换db.collection.bulkWrite(), which performs multiple write operations to a specific collection in a single call.db.collection.bulkWrite(),后者在一次调用中对特定集合执行多个写入操作。
Note
You can only use 您只能在MongoDB 8.0或更高版本中使用Mongo.bulkWrite() with MongoDB 8.0 or later.Mongo.bulkWrite()。
Syntax语法
You can call 您可以使用以下语法在当前bulkWrite() on the current Mongo() instance by using the following syntax: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
}
)
You can also call it on a different 您还可以在不同的Mongo instance, like in the following example:Mongo实例上调用它,如下例所示:
const otherMongo = Mongo("<other connection string>");
otherMongo.bulkWrite([{ namespace: "<db.collection>", ... }]);
bulkWrite() accepts two parameters:接受两个参数:
operations | ||
options |
A document in operations can represent one of six operations:operations中的文档可以表示六种操作之一:
- insert one
- replace one
- update one
- update many
- delete one
- delete many
The following sections describe the syntax you must use for documents that represent each operation.以下部分描述了表示每个操作的文档必须使用的语法。
Insert One插入一个
{
namespace: '<db.collection>',
name: 'insertOne',
document: Document
}
namespace | ||
name | "insertOne" for an insert one operation."insertOne"进行插入操作。 | |
document |
Note
If you do not specify an 如果不在文档中指定要插入的_id field in the document to insert, mongosh automatically generates an _id._id字段,mongosh会自动生成_id。
Update One or Many更新一个或多个
{
namespace: '<db>.<collection>',
name: 'updateOne' | 'updateMany',
filter: Document,
update: Document | Document[],
arrayFilters?: Document[],
hint?: Document | string,
collation?: Document,
upsert?: boolean
}
namespace | ||
name | "updateOne" for an update one operation. Set to "updateMany" for an update many operation."updateOne"进行更新一条数据操作。设置为"updateMany"进行更新多条数据操作。 | |
filter | mongosh only updates the first matching document. During an update many operation, mongosh updates all documents that match the filter.mongosh只更新第一个匹配的文档。在更新多个操作期间,mongosh会更新所有与筛选器匹配的文档。 | |
update | ||
arrayFilters | arrayFilters is a set of filters that specify which array elements an update applies to. | |
hint | ||
collation | ||
upsert | false.false。 |
Replace One替换一个
{
namespace: '<db>.<collection>',
name: 'replaceOne',
filter: Document,
replacement: Document,
hint?: Document | string,
collation?: Document
}
namespace | ||
name | "replaceOne" for a replace one operation."replaceOne"进行更换一次操作。 | |
filter | mongosh only replaces the first matching document.mongosh只替换第一个匹配的文档。 | |
replacement | ||
hint | ||
collation |
Delete One or Many删除一个或多个
{
namespace: '<db>.<collection>',
name: 'deleteOne' | 'deleteMany',
filter: Document,
hint?: Document | string,
collation?: Document
}
namespace | ||
name | "deleteOne" for a delete one operation. Set to "deleteMany" for a delete many operation."deleteOne"进行删除操作。设置为"deleteMany"进行删除多个操作。 | |
filter | mongosh only deletes the first matching document. mongosh只删除第一个匹配的文档。mongosh deletes all matching documents.mongosh会删除所有匹配的文档。 | |
hint | ||
collation |
Options选项
You can use the following options with 您可以在bulkWrite(). You can pass a document to bulkWrite() that contains the options that you want to use. This document is optional.bulkWrite()中使用以下选项。您可以将包含要使用的选项的文档传递给bulkWrite()。此文档是可选的。
{
ordered?: boolean,
verboseResults?: boolean,
bypassDocumentValidation?: boolean,
let?: Document
}
ordered | true.true。 | |
verboseResults | bulkWrite() outputs verbose results. Defaults to false.bulkWrite()是否输出详细结果。默认为false。 | |
bypassDocumentValidation | false.false。 | |
let |
Output输出
bulkWrite() returns an object with the following fields:返回一个包含以下字段的对象:
{
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 if the server returns an acknowledgment, false otherwise.true,否则为false。 | |
insertedCount | ||
matchedCount | ||
modifiedCount | ||
deletedCount | ||
upsertedCount | ||
insertResults |
| |
updateResults |
| |
deleteResults |
|
Examples示例
This example uses 此示例使用Mongo.bulkWrite() to perform the following operations in order:Mongo.bulkWrite()按顺序执行以下操作:
inserts a document into the将文档插入到db.authorscollectiondb.authors集合中inserts a document into the将文档插入db.bookscollectiondb.books集合中updates the previous document更新上一个文档
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 performs the bulk write in order and returns the following document:按顺序执行批量写入并返回以下文档:
{
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 } }
}