Query.prototype.deleteMany()

Parameters
  • [filter] «Object|Query» mongodb selector

  • [options] «Object» optional see Query.prototype.setOptions()

  • [callback] «Function» optional params are (error, mongooseDeleteResult)

Returns:
  • «Query» this

Declare and/or execute this query as a deleteMany() operation. Works like remove, except it deletes every document that matches filter in the collection, regardless of the value of single.

This function triggers deleteMany middleware.

Example

  1. await Character.deleteMany({ name: /Stark/, age: { $gte: 18 } });
  2. // Using callbacks:
  3. Character.deleteMany({ name: /Stark/, age: { $gte: 18 } }, callback);

This function calls the MongoDB driver’s Collection#deleteMany() function. The returned promise resolves to an object that contains 3 properties:

  • ok: 1 if no errors occurred
  • deletedCount: the number of documents deleted
  • n: the number of documents deleted. Equal to deletedCount.

Example

  1. const res = await Character.deleteMany({ name: /Stark/, age: { $gte: 18 } });
  2. // `0` if no docs matched the filter, number of docs deleted otherwise
  3. res.deletedCount;