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

Discriminators cause fields in subdocuments to be deselected by default #5775

Closed
walterrw opened this issue Nov 1, 2017 · 3 comments
Closed
Assignees
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@walterrw
Copy link

walterrw commented Nov 1, 2017

Using discriminators on subdocument schema causes fields to be deselected in query.

var itemSchema = new Schema({ 
  type: String, 
  active: { type: Boolean, default: true }
}, {
  discriminatorKey: 'type'
});

var collectionSchema = new Schema({ 
  items: [itemSchema] 
});

collectionSchema.path('items').discriminator('type1', new Schema({ count: Number }));

var collection = mongoose.model('Collection', collectionSchema);
collection.create({ 
  items: [ 
    { type: 'type1', active: false, count: 3 }
  ]
});

If I execute the query

collection.find().select('items');

I'd expect the result to be { :items [{ type: 'type1', active: false, count: 3 }]}.
Instead, it returns {:items [{ type: 'type1' }]}.

When I don't specify any fields for the query it behaves as expected.


mongoDB version: 3.4.1
node version: 8.5.0
mongoose version: 4.12.5

@vkarpov15 vkarpov15 modified the milestones: 4.12.6, 4.13.1 Nov 2, 2017
@vkarpov15
Copy link
Collaborator

Thanks for reporting, will investigate ASAP

@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Nov 2, 2017
@sobafuchs
Copy link
Contributor

yup, that does seem to be happening:

const mongoose = require('mongoose');
const co = require('co');
mongoose.Promise = global.Promise;
const GITHUB_ISSUE = `gh-5775`;

const { Schema } = mongoose;

exec()
  .then(() => {
    console.log('successfully ran program');
    process.exit(0);
  })
  .catch(error => {
    console.error(`Error: ${error}\n${error.stack}`);
  });


function exec() {
  return co(function* () {
    yield mongoose.connect(`mongodb://localhost:27017/${GITHUB_ISSUE}`, { useMongoClient: true });
    var itemSchema = new Schema({
      type: String,
      active: { type: Boolean, default: true }
    }, {
        discriminatorKey: 'type'
      });

    var collectionSchema = new Schema({
      items: [itemSchema]
    });

    collectionSchema.path('items').discriminator('type1', new Schema({ count: Number }));

    var collection = mongoose.model('Collection', collectionSchema);
    yield collection.create({
      items: [
        { type: 'type1', active: false, count: 3 }
      ]
    });

    console.log((yield collection.find().select('items'))[0].items);
  });
}

I wonder if this has something to do with you using type as a field on the schema

@sobafuchs sobafuchs added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Nov 3, 2017
@walterrw
Copy link
Author

walterrw commented Nov 3, 2017

It happens also if I use the default discriminatorKey

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

3 participants