Query.prototype.findOneAndDelete()

Parameters
  • [conditions] «Object»
  • [options] «Object»

  • [options.rawResult] «Boolean» if true, returns the raw result from the MongoDB driver

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

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

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

Returns:
  • «Query» this

Issues a MongoDB findOneAndDelete command.

Finds a matching document, removes it, and passes the found document (if any) to the callback. Executes if callback is passed.

This function triggers the following middleware.

  • findOneAndDelete()

This function differs slightly from Model.findOneAndRemove() in that findOneAndRemove() becomes a MongoDB findAndModify() command, as opposed to a findOneAndDelete() command. For most mongoose use cases, this distinction is purely pedantic. You should use findOneAndDelete() unless you have a good reason not to.

Available options

  • sort: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
  • maxTimeMS: puts a time limit on the query - requires mongodb >= 2.6.0
  • rawResult: if true, resolves to the raw result from the MongoDB driver

Callback Signature

  1. function(error, doc) {
  2. // error: any errors that occurred
  3. // doc: the document before updates are applied if `new: false`, or after updates if `new = true`
  4. }

Examples

  1. A.where().findOneAndDelete(conditions, options, callback) // executes
  2. A.where().findOneAndDelete(conditions, options) // return Query
  3. A.where().findOneAndDelete(conditions, callback) // executes
  4. A.where().findOneAndDelete(conditions) // returns Query
  5. A.where().findOneAndDelete(callback) // executes
  6. A.where().findOneAndDelete() // returns Query