-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Base models do not honor select: false for discriminators #4991
Comments
I seem to be experiencing unexpected side effects from this change: I have a base model EDIT: Disregard my comment, the issue was that mongoose adds the discriminator key to the query under the hood. The solution was to use the |
I might have spoken too soon. Testing with both version 4.13.4 and the latest I have taken the original example in this issue and modified to reproduce the behaviour: const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const co = require('co');
const chalk = require('chalk');
mongoose.set('debug', true);
const GITHUB_ISSUE = `default-values-on-discriminator-documents`;
exec()
.then(() => {
console.log(chalk.green(`Successfully ran program`));
process.exit(0);
})
.catch(error => {
console.log(chalk.red(`Error: ${error}\n${error.stack}`));
process.exit(2);
});
function exec() {
return co(function* () {
const db = mongoose.createConnection(`mongodb://localhost:27017/${GITHUB_ISSUE}`);
const { Base, Discriminator } = createModels(db);
const { baseDoc, discrimDoc } = yield seedDb({ Base, Discriminator });
const baseDocsFromDbWithSlice = yield Base.find().slice('array', 1)
const baseDocsFromDb = yield Base.find()
console.log(baseDocsFromDbWithSlice); // Discriminator document returns default value for propA (and not propB), base document returns stored value - unexpected
console.log(baseDocsFromDb); // Discriminator and base document returns stored value for propA - expected
});
}
function seedDb(models) {
return co(function* () {
const { Base, Discriminator } = models;
yield [Base.remove({}), Discriminator.remove({})];
const baseDoc = yield Base.create({
propA: 'Hi',
array: ["a", "b"]
});
const discrimDoc = yield Discriminator.create({
propA: 'Hi',
propB: 'Hello',
array: ["a", "b"]
});
return { baseDoc, discrimDoc };
});
}
function createModels(db) {
const baseSchema = new mongoose.Schema({
propA: { type: String, default: 'default value' },
array: [{type: String}],
});
const Base = db.model('Base', baseSchema);
const discriminatorSchema = new mongoose.Schema({
propB: { type: String}
});
const Discriminator = Base.discriminator('Discriminator', discriminatorSchema);
return { Base, Discriminator };
} This behaviour changed in |
Thanks for reporting @bruun , will investigate asap 👍 |
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
When a base model is used to retrieve a discriminator model document, it does not honor
select: false
.If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
We should probably unselect those fields manually after fetching from the database, since mongoose doesn't know ahead of time which discriminators to select for.
Please mention your node.js, mongoose and MongoDB version.
node 6.9.2, mongoose 4.8.3, mongodb 3.4
The text was updated successfully, but these errors were encountered: