Query.prototype.findOne()

Parameters
  • [filter] «Object» mongodb selector

  • [projection] «Object» optional fields to return

  • [options] «Object» see setOptions()

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

Returns:
  • «Query» this

Declares the query a findOne operation. When executed, the first found document is passed to the callback.

Passing a callback executes the query. The result of the query is a single document.

  • Note: conditions is optional, and if conditions is null or undefined, mongoose will send an empty findOne command to MongoDB, which will return an arbitrary document. If you’re querying by _id, use Model.findById() instead.

This function triggers the following middleware.

  • findOne()

Example

  1. const query = Kitten.where({ color: 'white' });
  2. query.findOne(function (err, kitten) {
  3. if (err) return handleError(err);
  4. if (kitten) {
  5. // doc may be null if no document matched
  6. }
  7. });