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

Created on 31 Aug 2017  ·  3Comments  ·  Source: Automattic/mongoose

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

confirmed-bug

Most helpful comment

@wlingke , finding all the most subtle bugs in mongoose since 2014 :rocket: :1st_place_medal:

image

All 3 comments

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

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

@wlingke , finding all the most subtle bugs in mongoose since 2014 :rocket: :1st_place_medal:

image

Was this page helpful?
0 / 5 - 0 ratings

Related issues

p3x-robot picture p3x-robot  ·  3Comments

simonxca picture simonxca  ·  3Comments

ghost picture ghost  ·  3Comments

varunjayaraman picture varunjayaraman  ·  3Comments

Soviut picture Soviut  ·  3Comments