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:

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:

  1. Character.bulkWrite([
  2. {
  3. insertOne: {
  4. document: {
  5. name: 'Eddard Stark',
  6. title: 'Warden of the North'
  7. }
  8. }
  9. },
  10. {
  11. updateOne: {
  12. filter: { name: 'Eddard Stark' },
  13. // If you were using the MongoDB driver directly, you'd need to do
  14. // `update: { $set: { title: ... } }` but mongoose adds $set for
  15. // you.
  16. update: { title: 'Hand of the King' }
  17. }
  18. },
  19. {
  20. deleteOne: {
  21. {
  22. filter: { name: 'Eddard Stark' }
  23. }
  24. }
  25. }
  26. ]).then(res => {
  27. // Prints "1 1 1"
  28. console.log(res.insertedCount, res.modifiedCount, res.deletedCount);
  29. });

The supported operations are:

  • insertOne
  • updateOne
  • updateMany
  • deleteOne
  • deleteMany
  • replaceOne