Query.prototype.remove()

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

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

Returns:
  • «Query» this

Declare and/or execute this query as a remove() operation. remove() is deprecated, you should use deleteOne() or deleteMany() instead.

This function does not trigger any middleware

Example

  1. Character.remove({ name: /Stark/ }, callback);

This function calls the MongoDB driver’s Collection#remove() 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.remove({ name: /Stark/ });
  2. // Number of docs deleted
  3. res.deletedCount;

Note

Calling remove() creates a Mongoose query, and a query does not execute until you either pass a callback, call Query#then(), or call Query#exec().

  1. // not executed
  2. const query = Character.remove({ name: /Stark/ });
  3. // executed
  4. Character.remove({ name: /Stark/ }, callback);
  5. Character.remove({ name: /Stark/ }).remove(callback);
  6. // executed without a callback
  7. Character.exec();