Model.exists()

Parameters
  • filter «Object»
  • [callback] «Function» callback
Returns:
  • «Promise»

Returns true if at least one document exists in the database that matches the given filter, and false otherwise.

Under the hood, MyModel.exists({ answer: 42 }) is equivalent to MyModel.findOne({ answer: 42 }).select({ _id: 1 }).lean().then(doc => !!doc)

Example:

  1. await Character.deleteMany({});
  2. await Character.create({ name: 'Jean-Luc Picard' });
  3. await Character.exists({ name: /picard/i }); // true
  4. await Character.exists({ name: /riker/i }); // false

This function triggers the following middleware.

  • findOne()