Model.bulkWrite()
Parameters
ops «Array»
[ops.insertOne.document] «Object» The document to insert
[opts.updateOne.filter] «Object» Update the first document that matches this filter
[opts.updateOne.update] «Object» An object containing update operators
[opts.updateOne.upsert=false] «Boolean» If true, insert a doc if none match
[opts.updateOne.timestamps=true] «Boolean» If false, do not apply timestamps to the operation
[opts.updateOne.collation] «Object» The MongoDB collation to use
[opts.updateOne.arrayFilters] «Array» The array filters used in
update
[opts.updateMany.filter] «Object» Update all the documents that match this filter
[opts.updateMany.update] «Object» An object containing update operators
[opts.updateMany.upsert=false] «Boolean» If true, insert a doc if no documents match
filter
[opts.updateMany.timestamps=true] «Boolean» If false, do not apply timestamps to the operation
[opts.updateMany.collation] «Object» The MongoDB collation to use
[opts.updateMany.arrayFilters] «Array» The array filters used in
update
[opts.deleteOne.filter] «Object» Delete the first document that matches this filter
[opts.deleteMany.filter] «Object» Delete all documents that match this filter
[opts.replaceOne.filter] «Object» Replace the first document that matches this filter
[opts.replaceOne.replacement] «Object» The replacement document
[opts.replaceOne.upsert=false] «Boolean» If true, insert a doc if no documents match
filter
[options] «Object»
[options.ordered=true] «Boolean» If true, execute writes in order and stop at the first error. If false, execute writes in parallel and continue until all writes have either succeeded or errored.
[options.session=null] «ClientSession» The session associated with this bulk write. See transactions docs.
[options.w=1] «String|number» The write concern. See
Query#w()
for more information.[options.wtimeout=null] «number» The write concern timeout.
[options.j=true] «Boolean» If false, disable journal acknowledgement
[options.bypassDocumentValidation=false] «Boolean» If true, disable MongoDB server-side schema validation for all writes in this bulk.
[options.strict=null] «Boolean» Overwrites the
strict
option on schema. If false, allows filtering and writing fields not defined in the schema for all writes in this bulk.[callback] «Function» callback
function(error, bulkWriteOpResult) {}
Returns:
- «Promise» resolves to a
BulkWriteOpResult
if the operation succeeds
Sends multiple insertOne
, updateOne
, updateMany
, replaceOne
, deleteOne
, and/or deleteMany
operations to the MongoDB server in one command. This is faster than sending multiple independent operations (e.g. if you use create()
) because with bulkWrite()
there is only one round trip to MongoDB.
Mongoose will perform casting on all operations you provide.
This function does not trigger any middleware, neither save()
, nor update()
. If you need to trigger save()
middleware for every document use create()
instead.
Example:
Character.bulkWrite([
{
insertOne: {
document: {
name: 'Eddard Stark',
title: 'Warden of the North'
}
}
},
{
updateOne: {
filter: { name: 'Eddard Stark' },
// If you were using the MongoDB driver directly, you'd need to do
// `update: { $set: { title: ... } }` but mongoose adds $set for
// you.
update: { title: 'Hand of the King' }
}
},
{
deleteOne: {
{
filter: { name: 'Eddard Stark' }
}
}
}
]).then(res => {
// Prints "1 1 1"
console.log(res.insertedCount, res.modifiedCount, res.deletedCount);
});
The supported operations are:
insertOne
updateOne
updateMany
deleteOne
deleteMany
replaceOne