Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ahmedelshenawy25 opened this issue Dec 23, 2021 · 0 comments · Fixed by #11142
Closed

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

ahmedelshenawy25 opened this issue Dec 23, 2021 · 0 comments · Fixed by #11142
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@ahmedelshenawy25
Copy link
Contributor

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

AbdelrahmanHafez added a commit to AbdelrahmanHafez/mongoose that referenced this issue Dec 26, 2021
@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Dec 27, 2021
@vkarpov15 vkarpov15 added this to the 6.1.5 milestone Dec 30, 2021
@vkarpov15 vkarpov15 reopened this Dec 30, 2021
vkarpov15 added a commit that referenced this issue Dec 30, 2021
fix(model): make exists(...) return lean document with `_id` or `null` instead of boolean
@vkarpov15 vkarpov15 modified the milestones: 6.1.5, 6.2.0 Jan 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
3 participants