-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Default array not being hydrated when using dotted path projection #11376
Comments
const mongoose = require('mongoose');
const schema = new mongoose.Schema({
array: {
type: [{ nestedProperty: String }],
default: []
},
object: {
type: { nestedProperty: String },
default: {}
}
});
const Test = mongoose.model('Test', schema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
await Test.create({});
// OK, result: { array: [], object: {} }
console.log((await Test.findOne()));
// OK, result: { array: [], object: {} }
console.log((await Test.findOne().select({ 'array': true, 'object': true })));
// KO, result: { object: {} }, expected: { array: [], object: {} }
console.log((await Test.findOne().select({ 'array.nestedProperty': true, 'object.nestedProperty': true })));
}
run(); |
Hi, thanks for having taken a look. Remember, I said that there is no problem when the properties are set on the live document — eventually showing that it's not a problem with the projection for example, but rather with the default values. To reproduce, in your snippet, just replace this: await Test.create({}); With: await mongoose.connection.collection('tests').insertOne({}); On my side, I was able to reproduce the problem again like this. |
const mongoose = require('mongoose');
const schema = new mongoose.Schema({
array: {
type: [{ nestedProperty: String }],
default: []
},
object: {
type: { nestedProperty: String },
default: {}
}
});
const Test = mongoose.model('Test', schema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
await mongoose.connection.collection('tests').insertOne({})
// OK, result: { array: [], object: {} }
console.log((await Test.findOne()));
// OK, result: { array: [], object: {} }
console.log((await Test.findOne().select({ 'array': true, 'object': true })));
// KO, result: { object: {} }, expected: { array: [], object: {} }
console.log((await Test.findOne().select({ 'array.nestedProperty': true, 'object.nestedProperty': true })));
}
run(); |
Thanks @IslandRhythms @vkarpov15! |
Hello,
I think I hit a corner case of the default value hydration, combined with dotted path projection; they don't seem to play well with each other.
This demonstration should sum it up. I have an empty document in database, that I query with findOne(). In the third
console.log()
, where dotted path notation is used for the projection,array
property is no longer being populated with the default value, whileobject
still is (as expected).If I'm not mistaken this should not be the expected behavior.
Note: if the live document has the
array
property defined, everything works as expected.Mongoose v6.2.1 is being used, but I was able to reproduce the problem as far as v5.3, the oldest version that works with my project without making bigger changes — so this must be an old behaviour and not a recent regression.
Node 16.13.2.
The text was updated successfully, but these errors were encountered: