Query.prototype.deleteOne()

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 deleteOne() operation. Works like remove, except it deletes at most one document regardless of the single option.

This function triggers deleteOne middleware.

Example

  1. await Character.deleteOne({ name: 'Eddard Stark' });
  2. // Using callbacks:
  3. Character.deleteOne({ name: 'Eddard Stark' }, callback);

This function calls the MongoDB driver’s Collection#deleteOne() 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.deleteOne({ name: 'Eddard Stark' });
  2. // `1` if MongoDB deleted a doc, `0` if no docs matched the filter `{ name: ... }`
  3. res.deletedCount;