Skip to content

Model.exists() returns a promise and resolves to a boolean #11138

Closed
@ahmedelshenawy25

Description

@ahmedelshenawy25

Do you want to request a feature or report a bug?
Bug confirmed by @AbdelrahmanHafez

What is the current behavior?
Model.exists() returns a promise and resolves to a boolean

If the current behavior is a bug, please provide the steps to reproduce.

import mongoose from 'mongoose';
const { Schema } = mongoose;
import assert from 'assert';


run().catch(console.error);

async function run () {
  await mongoose.connect('mongodb://localhost:27017/test');

  await mongoose.connection.dropDatabase();

  const userSchema = new Schema({
    name: { type: String }
  });

  const User = mongoose.model('User', userSchema);
  const user = await User.create({});

  const existingUser = await User.exists({ _id: user._id });
  assert.ok(existingUser._id);
  assert.ok((typeof existingUser) !== 'boolean');

  console.log(mongoose.version);
  console.log('All assertions passed.');
}

What is the expected behavior?
Model.exists() should return a query and resolve to a doc or null #8097

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js: 16.13.1
Mongoose: 6.1.3
MongoDB: 5.0.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions