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

Saving a document that was fetched with .select() causes not selected array to be saved empty #12414

Closed
2 tasks done
Bene-Graham opened this issue Sep 9, 2022 · 2 comments · Fixed by #12506
Closed
2 tasks done
Assignees
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@Bene-Graham
Copy link

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.5.2

Node.js version

16.x

MongoDB server version

5.0.6

Description

This seems to have been changed from version 6.2.2 and 6.2.3. Still an issue in 6.6.0.

const dividerSchema = new mongoose.Schema({
    dividerKey: { type: String, default: "" }
}, { _id: false });

const dataSchema = new mongoose.Schema({
    sectionA: {
        include: { type: Boolean, default: true },
        dividers: [dividerSchema]
    },
    sectionB: {
        include: { type: Boolean, default: true },
        dividers: [dividerSchema],
    }
});

For the given schema if we try to retrieve a document

DataModel.findOne({ "_id": createdDocument._id }).select("sectionA.include sectionA.carrierId sectionB").exec();

and then imminently save it the following update query is created

data_xes.updateOne({ _id: new ObjectId("631ba9fbadbfff8b4ab1f888") }, { '$set': { 'sectionA.dividers': [] } }, { session: null })

This is weird since I never selected or have touch that value.

Steps to Reproduce

import * as mongoose from "mongoose";

const connectToMongo = async () => {
    mongoose.set("debug", true);
    await mongoose.connect("", {
        autoIndex: false,
        autoCreate: false
    });
}

const dividerSchema = new mongoose.Schema({
    dividerKey: { type: String, default: "" }
}, { _id: false });

const dataSchema = new mongoose.Schema({
    sectionA: {
        include: { type: Boolean, default: true },
        dividers: [dividerSchema]
    },
    sectionB: {
        include: { type: Boolean, default: true },
        dividers: [dividerSchema],
    }
});

export const DataModel = mongoose.model("data_x", dataSchema);

const doWork = async () => {
    const createdDocument = await DataModel.create({
        sectionA: {
            include: true,
            dividers: [
                {
                    dividerKey: "SOME_DIVIDER_KEY"
                },
                {
                    dividerKey: "SOME_DIVIDER_KEY"
                }
            ]
        },
        sectionB: {
            include: true,
            dividers: [
                {
                    dividerKey: "SOME_DIVIDER_KEY"
                },
                {
                    dividerKey: "SOME_DIVIDER_KEY"
                }
            ]
        }
    });

    const doc = await DataModel.findOne({ "_id": createdDocument._id }).select("sectionA.include sectionA.carrierId sectionB").exec();

    await doc.save();
};

connectToMongo()
.then(async () => await doWork())
.then(async () => {
    await mongoose.disconnect();
    process.exit(0);
})
.catch((err) => {
    console.error(err);
    process.exit(-1);
});

Expected Behavior

I would expect non selected sub array not to update.

@IslandRhythms

This comment was marked as off-topic.

@Bene-Graham
Copy link
Author

It seems like the non selected array are being defaulted to empty arrays.

const doc = await Test.findOne({ "_id": createdDocument._id }).select("sectionA.include sectionA.carrierId sectionB").exec();

// 6.2.2 undefined
// 6.2.3 []
console.log(doc.sectionA.dividers);

@vkarpov15 vkarpov15 added this to the 6.6.4 milestone Sep 30, 2022
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue 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 Sep 30, 2022
vkarpov15 added a commit that referenced this issue Oct 3, 2022
vkarpov15 added a commit that referenced this issue Oct 3, 2022
fix(model): avoid saving applied defaults if path is deselected
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

Successfully merging a pull request may close this issue.

3 participants