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

findByIdAndUpdate + .select returns only _id on v >= 6.9.3, < 7.0.0 #13340

Closed
2 tasks done
draperunner opened this issue Apr 28, 2023 · 0 comments
Closed
2 tasks done
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@draperunner
Copy link

draperunner commented Apr 28, 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

6.9.3

Node.js version

16

MongoDB server version

4.4

Typescript version (if applicable)

No response

Description

After upgrading to the latest version of mongoose v6.x, I noticed that only the _id field was returned from one of my update statements, resulting in errors trying to access data on undefined subdocuments.

In my case, I am using select("+optionalfield"), but it seems any projection fails.

const updated = await Kitten.findByIdAndUpdate(silence._id, { age: 3 })
  .select("+age")

console.log("UPDATED", updated) // <-- Returns _id only, not name or age!

Result:

{ _id: new ObjectId("644c3025179705584f9945aa") }

Expected result:

{
  _id: new ObjectId("644c3025179705584f9945aa"),
  name: 'Silence',
  age: 2,
  __v: 0
}

I have reproduced this on different versions of mongoose and it seems that v6.9.3 introduced this error.

Upgrading to v7 has fixed it for me, but I guess you might want to release a fix on v6.

Steps to Reproduce

Install mongoose version 6.x and run the following code:

const mongoose = require('mongoose');

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

  const kittySchema = new mongoose.Schema({
      name: String,
      age: {
        select: false,
        type: Number
      }
    });

  const Kitten = mongoose.model('Kitten', kittySchema);
  const silence = new Kitten({ name: 'Silence', age: 2 });
  await silence.save()

  const updated = await Kitten.findByIdAndUpdate(silence._id, { age: 3 })
    .select("+age")

  console.log("UPDATED", updated) // <-- Returns _id only, not name or age!
}

main()

Expected Behavior

The following code

const updated = await Kitten.findByIdAndUpdate(silence._id, { age: 3 })
  .select("+age")

console.log(updated)

should return:

{
  _id: new ObjectId("644c3025179705584f9945aa"),
  name: 'Silence',
  age: 2,
  __v: 0
}

but instead returns:

{ _id: new ObjectId("644c3025179705584f9945aa") }
@vkarpov15 vkarpov15 added this to the 6.11.0 milestone May 1, 2023
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels May 1, 2023
vkarpov15 added a commit that referenced this issue May 1, 2023
@vkarpov15 vkarpov15 modified the milestones: 6.11.0, 6.11.1 May 1, 2023
vkarpov15 added a commit that referenced this issue May 2, 2023
fix(query): apply schema-level paths before calculating projection for findOneAndUpdate()
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

No branches or pull requests

2 participants