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

Projection with '+' using findByIdAndUpdate does not return the property #13413

Closed
2 tasks done
sspenst opened this issue May 18, 2023 · 2 comments · Fixed by #13437
Closed
2 tasks done

Projection with '+' using findByIdAndUpdate does not return the property #13413

sspenst opened this issue May 18, 2023 · 2 comments · Fixed by #13437
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@sspenst
Copy link

sspenst commented May 18, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

7.1.1

Node.js version

18.7.0

MongoDB server version

5.5.0

Typescript version (if applicable)

5.0.4

Description

When using select: false for a property x in a schema, x is not returned when using findByIdAndUpdate and projection: '+x'.

Only started occurring in 7.1.1, I don't see this issue <= 7.1.0.

Steps to Reproduce

In a schema:

x: {
  type: String,
  select: false,
},

Then try to do:

const response = await XModel.findByIdAndUpdate(
  id,
  { $set: { y: y } },
  { projection: '+x' },
);

response does not contain x.

Expected Behavior

The property is returned.

@sspenst
Copy link
Author

sspenst commented May 18, 2023

Maybe related to #13340

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label May 22, 2023
@IslandRhythms
Copy link
Collaborator

Tested on 7.2

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
  name: String,
  nickName: {
    type: String,
    select: false
  }
});

const Test = mongoose.model('Test', testSchema);

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

  const entry = await Test.create({
    name: 'Test Testerson',
    nickName: 'Quiz'
  });

 const res = await Test.findByIdAndUpdate(entry._id, { $set: { name: 'Test' } }, { projection: '+nickName', returnDocument: 'after'});
  console.log(res);
}

run();

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
Development

Successfully merging a pull request may close this issue.

3 participants