-
-
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
findOneAndUpdate discriminator key does not survive projection #11546
Comments
@OoDeLally Modify the script below to demonstrate your issue. If I had to guess, you were doing the const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
name: String,
});
const Test = mongoose.model('Test', testSchema);
const TestScrim = Test.discriminator('TestScrim', new mongoose.Schema({
age: Number
}, {discriminatorKey: 'kind'}));
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
await Test.create({
name: 'Batman'
});
await TestScrim.create({
name: 'Nightwing',
age: 18
});
await TestScrim.create({
name: 'Batman',
age: 36
})
console.log(await Test.find({name: 'Batman'}, {name: 1}));
const result = await TestScrim.findOneAndUpdate({age: 36}, {name: 'batman'}, {name: 1, new: true})
console.log(result);
}
run(); |
Hello @IslandRhythms, thank you for your prompt answer. I found out that it happens when the discriminator key is part of the schema: const mongoose = require('mongoose');
const baseSchema = new mongoose.Schema({
kind: { type: String }, // Comment out to get back to normal behavior.
name: String,
}, {discriminatorKey: 'kind'});
const BaseModel = mongoose.model('Base', baseSchema);
const childSchema = new mongoose.Schema({
age: Number
}, { discriminatorKey: 'kind' });
const ChildModel = BaseModel.discriminator('Child', childSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
await ChildModel.create({
name: 'Batman',
age: 36
})
const findResult = await BaseModel.findOne({age: 36}, {name: 1})
console.log('findResult');
console.log(findResult);
const findOneAndUpdateResult = await BaseModel.findOneAndUpdate({age: 36}, {name: 'batman'}, {new: true, projection: { name: 1 }})
console.log('findOneAndUpdateResult');
console.log(findOneAndUpdateResult);
await mongoose.connection.close();
}
run(); What is mongoose's recommendation regarding adding the discriminator to the schema ? I use typescript enums as discriminator values, and therefore it is useful for me to get the validation. |
If you mean defining the discriminator on the schema, it is a feature that will be shipped in the nearby future, possible in mongoose 6.3. |
In #4629 we made it so that const findResult = await BaseModel.findOne({age: 36}, {name: 1, __t: 0}) to opt out of automatically selecting the discriminator key. |
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
When using a discriminator key, all mongoose methods return the discriminator key in addition of the provided projection.
e.g. A
{name: 1}
projection would still result in a document including the discriminator{name: 'Batman', kind: 'Hero'}
.findOneAndUpdate
differs from this behavior, in that it does not include the discriminator key.e.g.
{name: 1}
projection results in{name: 'Batman'}
.mongoose 6.1.5
The text was updated successfully, but these errors were encountered: