Skip to content

Cannot patch a sub-document with destructuring #14269

@a88zach

Description

@a88zach

Prerequisites

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

Mongoose version

8.1.0

Node.js version

20.11.0

MongoDB server version

7.x

Typescript version (if applicable)

5.3.3

Description

When patching a sub-document with destructuring, the changes are not saved correctly.

const person = await personModel.findOne({...});

person.address = {
      ...person.address,
      zip: 54321,
    };

    await person.save();

However, after saving (and even before the save is called) the person.address.zip is still the old value.

Steps to Reproduce

Cone this repo: https://github.com/a88zach/mongoose-bug

run $ yarn test

Notice the 1 test that is successful and the 1 test that fails. The one that fails is using destructuring to patch the sub document

Expected Behavior

The sub-document should be updated with the existing information plus any overrides based on the patch

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions