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

select: false does not work as part of a Child Schema if Child Schema is used more than once #5603

Closed
wlingke opened this issue Aug 31, 2017 · 3 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@wlingke
Copy link
Contributor

wlingke commented Aug 31, 2017

Do you want to request a feature or report a bug?
Bug

What is the current behavior?

In the script below, when we pull the parent document, we see that child comes back as {}, but child2 comes back as { field: 'test'} despite the fact that ChildSchema.field has select: false.

When looking at the debugger, only child.field is being selected out and child2.field is not.



const mongoose = require('mongoose');

mongoose.connect('mongodb://127.0.0.1/test');
const db = mongoose.connection;

db.once('open', async () => {
  try {
    const ChildSchema = new mongoose.Schema({
      field: {
        type: String,
        select: false,
      },
      _id: false,
    }, { id: false });

    const ParentSchema = new mongoose.Schema({
      child: ChildSchema,
      child2: ChildSchema,
    });
    const Parent = mongoose.model('Parent', ParentSchema);
    const ogParent = new Parent();
    ogParent.child = { field: 'test' };
    ogParent.child2 = { field: 'test' };
    await ogParent.save();

    const foundParent = await Parent.findById(ogParent._id).exec();
    console.log(foundParent);

    db.close();
  } catch (e) {
    console.log(e);
  }
});


Please mention your node.js, mongoose and MongoDB version.
4.11.8

@sobafuchs
Copy link
Contributor

thanks for the full repro script, this looks like a bug

@sobafuchs sobafuchs added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Sep 6, 2017
@sobafuchs sobafuchs added this to the 4.11.12 milestone Sep 6, 2017
@heisian
Copy link

heisian commented Sep 6, 2017

Giving @wlingke an imaginary badge for Best Mongoose Bug Finder of the Year, 2017™ 🐜

vkarpov15 added a commit that referenced this issue Sep 15, 2017
@vkarpov15
Copy link
Collaborator

@wlingke , finding all the most subtle bugs in mongoose since 2014 🚀 🥇

image

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

4 participants