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

Document set merge doesn't work for multi layer nested documents #14861

Closed
2 tasks done
ianHeydoc opened this issue Sep 3, 2024 · 3 comments
Closed
2 tasks done

Document set merge doesn't work for multi layer nested documents #14861

ianHeydoc opened this issue Sep 3, 2024 · 3 comments
Milestone

Comments

@ianHeydoc
Copy link
Contributor

ianHeydoc commented Sep 3, 2024

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

Node.js version

18.15.0

MongoDB server version

NA

Typescript version (if applicable)

No response

Description

When using document set with the merge option set to true if there are multiple nested fields the deeply nested fields are ignored

Steps to Reproduce

const { Schema, model } = require('mongoose');

const personSchema = new Schema({
  info: {
    address: {
      city: String,
      country: { type: String, default: "UK" },
      postcode:String
    },
  }
});

const personModel = model("collection", personSchema);
const document = new personModel({
  info: {
    address: { country: "United States", city: "New York" },
  }
});
console.log(document);

document.$set({ info: { address: { postcode: "12H" } } }, null, { merge: true });

console.log(document);

Expected Behavior

For this code example I get

{
  info: { address: { postcode: '12H', country: 'UK' } },
  _id: new ObjectId('66d693ee34802938adef5702')
}

I would expect to get

{
  info: { address: { city: 'New York', country: 'United States',  postcode: '12H' } },
  _id: new ObjectId('66d692b9c9fe017af8ab4f78')
}
@ianHeydoc
Copy link
Contributor Author

I've taken a look into the code and found this code snippet

this is on line 1212 of mongoose/lib/document.js

      if (!merge) {
        this.$__setValue(path, null);
        cleanModifiedSubpaths(this, path);
      } else {
        return this.$set(val, path, constructing);
      }

It looks like the merge option is lost when the second nested object is set

I've tested out adding the options to the next $set call and it looks like it works 👀

Would this be something I could open a pr to implement or is there a reason for the behaviour?

I'm new to looking at the source code for mongoose so any help would be appreciated 😃

@pdrf
Copy link

pdrf commented Sep 3, 2024

+1

vkarpov15 added a commit that referenced this issue Sep 9, 2024
set merges deeply nested objects
@vkarpov15
Copy link
Collaborator

Should be fixed by #14870

@vkarpov15 vkarpov15 added this to the 8.6.2 milestone Sep 9, 2024
vkarpov15 added a commit that referenced this issue Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants