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

Default array not being hydrated when using dotted path projection #11376

Closed
toverux opened this issue Feb 10, 2022 · 4 comments
Closed

Default array not being hydrated when using dotted path projection #11376

toverux opened this issue Feb 10, 2022 · 4 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@toverux
Copy link

toverux commented Feb 10, 2022

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, while object still is (as expected).

const schema = new mongoose.Schema({
    array: {
        type: [{ nestedProperty: String }],
        default: []
    },

    object: {
        type: { nestedProperty: String },
        default: {}
    }
});

const model = mongoose.model('Model', schema);

// OK, result: { array: [], object: {} }
console.log((await model.findOne()));

// OK, result: { array: [], object: {} }
console.log((await model.findOne().select({ 'array': true, 'object': true })));

// KO, result: { object: {} }, expected: { array: [], object: {} }
console.log((await model.findOne().select({ 'array.nestedProperty': true, 'object.nestedProperty': true })));

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.

@IslandRhythms IslandRhythms added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Feb 10, 2022
@IslandRhythms
Copy link
Collaborator

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();

@toverux
Copy link
Author

toverux commented Feb 11, 2022

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.
When using Test.create() in your test case, defaults are being set on the document before it's even created, so everything works as expected because default value hydration doesn't happen with find() and its projection, but before insertion.

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.

@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Feb 11, 2022
@IslandRhythms
Copy link
Collaborator

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();

@vkarpov15 vkarpov15 added this to the 6.2.3 milestone Feb 12, 2022
@toverux
Copy link
Author

toverux commented Feb 20, 2022

Thanks @IslandRhythms @vkarpov15!

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