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

How to check modified state on nested schemas #7048

Closed
jsardev opened this issue Sep 24, 2018 · 1 comment
Closed

How to check modified state on nested schemas #7048

jsardev opened this issue Sep 24, 2018 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@jsardev
Copy link

jsardev commented Sep 24, 2018

Do you want to request a feature or report a bug?

A bug, I guess.

What is the current behavior?

doc.modifiedPaths() returns that paths have been modified (even if they didn't) on models that have nested schemas. What is also interesting: it works completely fine when i don't do type: ChildSchema and just embed the schema inline.

If the current behavior is a bug, please provide the steps to reproduce.

const BarSchema = new Schema(
  {
    bar: {
      type: String
    }
  },
  { _id: false }
);

const FooNestedSchema = new Schema({
  foo: {
    type: BarSchema
  }
});

const FooSchema = new Schema({
  foo: {
    bar: {
      type: String
    }
  }
});

const FooNested = mongoose.model('fooNested', FooNestedSchema);
const fooNested = new FooNested({ foo: { bar: 'value' } });

const Foo = mongoose.model('foo', FooSchema);
const foo = new Foo({ foo: { bar: 'value' } });

(async () => {
  await fooNested.save();
  await foo.save();

  fooNested.set({ foo: { bar: 'value' } });
  foo.set({ foo: { bar: 'value' } });

  fooNested.modifiedPaths(); // WRONG: [ 'foo', 'foo.bar' ]
  foo.modifiedPaths(); // CORRECT: []
})();

What is the expected behavior?

It would be a lot less confusing if both examples would behave the same.

Please mention your node.js, mongoose and MongoDB version.

  • Node.js: 10.9.0
  • Mongoose: 5.2.17

Additional info

Is there any workaround for this issue? Can I somehow manually check for equality on a saved document? schema.pre('validate', () => {}) gives me only access to the already changed document, which is already a bummer. Any ideas?

@vkarpov15
Copy link
Collaborator

Thanks for reporting, will look into this ASAP. As a workaround, you can use a deep equality check like lodash.deepEquals(), just make sure you call toObject() on both documents that you're comparing.

@vkarpov15 vkarpov15 added this to the 5.2.20 milestone Sep 28, 2018
@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Sep 28, 2018
vkarpov15 added a commit that referenced this issue Oct 4, 2018
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

2 participants