Model.findByIdAndRemove()

Parameters
  • id «Object|Number|String» value of _id to query by

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

  • [options.strict] «Boolean|String» overwrites the schema’s strict mode option

  • [options.session=null] «ClientSession» The session associated with this query. See transactions docs.

  • [options.projection=null] «Object|String|Array<String>» optional fields to return, see Query.prototype.select()

  • [callback] «Function»

Returns:
  • «Query»

Issue a mongodb findAndModify remove command by a document’s _id field. findByIdAndRemove(id, ...) is equivalent to findOneAndRemove({ _id: id }, ...).

Finds a matching document, removes it, passing the found document (if any) to the callback.

Executes the query if callback is passed.

This function triggers the following middleware.

  • findOneAndRemove()

Options:

  • sort: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
  • select: sets the document fields to return
  • rawResult: if true, returns the raw result from the MongoDB driver
  • strict: overwrites the schema’s strict mode option for this update

Examples:

  1. A.findByIdAndRemove(id, options, callback) // executes
  2. A.findByIdAndRemove(id, options) // return Query
  3. A.findByIdAndRemove(id, callback) // executes
  4. A.findByIdAndRemove(id) // returns Query
  5. A.findByIdAndRemove() // returns Query