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

Arrays on nested object are emptied on save if an other object of the same parent object is in projection #13043

Closed
2 tasks done
Getz85 opened this issue Feb 17, 2023 · 2 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@Getz85
Copy link

Getz85 commented Feb 17, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.9.2

Node.js version

18.12.1

MongoDB server version

4.2.23

Typescript version (if applicable)

No response

Description

If a projection is made on a nested object on find, the arrays defined in the parent object are emptied on save.

Steps to Reproduce

const mongoose = require("mongoose");
(async () => {
    try {
        await mongoose.connect("mongodb://127.0.0.1:27017/test");
        // Define a schema wich contains an object (obj) composed of an object (subObj) and an array (subArr)
        const testSchema = new mongoose.Schema({
            str: 'string',
            obj: {
                subObj: {
                    str: 'string'
                },
                subArr: [{
                    str: 'string'
                }]
            },
            arr: [{
                str: 'string'
            }]
        });
        const Test = mongoose.model('Test', testSchema);
        // Create one test document : obj.subArr[0].str === 'subArr.test1'
        await Test.create({
            str: 'test1',
            obj: {
                subObj: {
                    str: 'subObj.test1'
                },
                subArr: [{
                    str: 'subArr.test1'
                }]
            },
            arr: [{str: 'arr.test1'}]
        });
        // Find the document with a obj.subObj projection
        const tests = await Test.find({str: 'test1'}, "str obj.subObj");

        for (const test of tests) {
            // Update one property
            test.str = test.str + ' - updated';
            await test.save();
            // obj.subArr is empty in database
        }
        process.exit(0);
    } catch (err) {
        console.error(err);
        process.exit(1);
    }
})();

Expected Behavior

The arrays not projected must not be emptied on save

@vkarpov15 vkarpov15 added this to the 6.9.3 milestone Feb 17, 2023
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Feb 17, 2023
@vkarpov15 vkarpov15 modified the milestones: 6.9.3, 6.9.5 Feb 21, 2023
@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Feb 27, 2023
@IslandRhythms
Copy link
Collaborator

const mongoose = require("mongoose");
(async () => {
    try {
        await mongoose.connect("mongodb://127.0.0.1:27017/test");
        await mongoose.connection.dropDatabase();
        // Define a schema wich contains an object (obj) composed of an object (subObj) and an array (subArr)
        const testSchema = new mongoose.Schema({
            str: 'string',
            obj: {
                subObj: {
                    str: 'string'
                },
                subArr: [{
                    str: 'string'
                }]
            },
            arr: [{
                str: 'string'
            }]
        });
        const Test = mongoose.model('Test', testSchema);
        // Create one test document : obj.subArr[0].str === 'subArr.test1'
        await Test.create({
            str: 'test1',
            obj: {
                subObj: {
                    str: 'subObj.test1'
                },
                subArr: [{
                    str: 'subArr.test1'
                }]
            },
            arr: [{str: 'arr.test1'}]
        });
        // Find the document with a obj.subObj projection
        const tests = await Test.find({str: 'test1'}, "str obj.subObj");

        for (const test of tests) {
            // Update one property
            test.str = test.str + ' - updated';
            await test.save();
            // obj.subArr is empty in database
        }
        console.log(await Test.find())
        process.exit(0);
    } catch (err) {
        console.error(err);
        process.exit(1);
    }
})();

@vkarpov15
Copy link
Collaborator

Confirmed that this bug is fixed by #13135

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