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

.toObject() has different behavior from 6.x to 7.x #13550

Closed
2 tasks done
puskin94 opened this issue Jun 28, 2023 · 1 comment · Fixed by #13572
Closed
2 tasks done

.toObject() has different behavior from 6.x to 7.x #13550

puskin94 opened this issue Jun 28, 2023 · 1 comment · Fixed by #13572
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@puskin94
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

7.x

Node.js version

18.15

MongoDB server version

6.x

Typescript version (if applicable)

5.1.3

Description

The .toObject() method called after the creation of a document has a different behavior when you compare mongoose version 6.x and 7.x

It looks like the deeply nested properties , when set to undefined, and passed to a findByIdAndUpdate in:

  • version 6.x are removed
  • version 7.x are set to null

removing the toObject() method call make the 2 mongoose versions behave the same way, as displayed in the steps to Reproduce

Steps to Reproduce

const mongoose = require('mongoose');

const mySchema = new mongoose.Schema({
    extended: {
        type:    Object || String,
        default: {keep: true}
    }
});

const myModel = mongoose.model('my', mySchema);

async function main() {
    await mongoose.connect('mongodb://localhost:27017/test-mongoose');

    const myObj = {
        extended: {
            something: {else: true},
            level1:    {level2: {prop: 'something'}}
        }
    };

    let added = (await myModel.create(myObj)).toObject(); // Without the toObject both versions behave the same
    console.log('>>> added:', added.extended); // { something: { else: true }, level1: { level2: { prop: something } } }

    added.extended.level1.level2.prop = undefined;
    console.log('>>> updated', added.extended); // { something: { else: true }, level1: { level2: { prop: undefined } } }

    await myModel.findByIdAndUpdate(added._id, added, {lean: true, new: true, overwrite: true});

    const found = await myModel.findById(added._id);
    console.log('>>> found:', found.extended);
    // 7.x.x { something: { else: true }, level1: { level2: { prop: null } } }
    // 6.x.x { something: { else: true } }
}

main()
    .then(() => process.exit())
    .catch(e => {
        console.error(e);
        process.exit(1);
    });

These are the logs for version 6.x with the toObject() method call:
6x with toObject

These are the logs for version 7.x with the toObject() method call:
7x with toObject

These are the logs for version 7.x without the toObject() method call:
7x without toObject

Expected Behavior

No response

@vkarpov15 vkarpov15 added this to the 7.3.2 milestone Jul 2, 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 Jul 2, 2023
vkarpov15 added a commit that referenced this issue Jul 3, 2023
…ite set for backwards compat with Mongoose 6

Fix #13550
@vkarpov15
Copy link
Collaborator

The fundamental issue here is that, in Mongoose 6, findOneAndUpdate() with overwrite: true executes a findOneAndReplace(). We'll add that behavior back to Mongoose 7, but I would recommend you use findOneAndReplace() instead of findOneAndUpdate() with overwrite. We may deprecate the overwrite option in the future.

@vkarpov15 vkarpov15 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 Jul 3, 2023
vkarpov15 added a commit that referenced this issue Jul 4, 2023
fix(query): convert findOneAndUpdate to findOneAndReplace when overwrite set for backwards compat with Mongoose 6
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
2 participants