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

Schema.isModified not always accurate for subschema paths #8223

Closed
quinnlas opened this issue Oct 7, 2019 · 1 comment
Closed

Schema.isModified not always accurate for subschema paths #8223

quinnlas opened this issue Oct 7, 2019 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@quinnlas
Copy link

quinnlas commented Oct 7, 2019

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

What is the current behavior?
With a schema like so:

const secondLevelSchema = new Schema({
  id: Number,
  text: String
})

const topLevelSchema = new Schema({
  secondLevel: {
    type: secondLevelSchema
  }
}

If you create a new topLevelItem and set one of the properties of secondLevel but not the other,
for instance:
new TopLevelModel({ secondLevel: { text: 'example' } })

then you will have the following strange behavior for the other property:

topLevelItem.isModified('secondLevel.id') //true
topLevelItem.secondLevel.isModified('id')) //false

What is the expected behavior?
both values should be false, since the id has not been modified

@vkarpov15 vkarpov15 added this to the 5.7.5 milestone Oct 9, 2019
@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 Oct 9, 2019
vkarpov15 added a commit that referenced this issue Oct 11, 2019
@vkarpov15 vkarpov15 reopened this Oct 13, 2019
@vkarpov15
Copy link
Collaborator

We fixed it so that both topLevelItem.isModified('secondLevel.id') and topLevelItem.secondLevel.isModified('id')) return true. The reason this behavior is that new TopLevelModel({ secondLevel: { text: 'example' } }) sets secondLevel, so secondLevel is modified and therefore all of its children are modified as well. This is because isModified() exists primarily to help save() figure out what paths were updated.

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