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

fix(query): apply translateAliases before casting to avoid strictMode error when using aliases #14562

Merged
merged 1 commit into from
May 6, 2024

Conversation

vkarpov15
Copy link
Collaborator

Fix #14521

Summary

The following code errors out with a "n" is not in the schema error because of upsert. The issue is that we first cast the query filter, then run translateAliases, which is a 2-fold bug: 1) alias isn't in schema so Mongoose throws strict mode error with "upsert", 2) Mongoose doesn't apply casting to fields where the user specifies the path's alias rather than the path itself.

    const doc = await Test.findOneAndUpdate(
      { n: '14521' },
      { age: 7511 },
      { translateAliases: true, upsert: true, returnDocument: 'after' }
    );

With this PR, translateAliases runs before query casting.

Examples

@vkarpov15 vkarpov15 added this to the 8.3.4 milestone May 6, 2024
@vkarpov15 vkarpov15 merged commit d24b7a8 into master May 6, 2024
33 of 34 checks passed
@hasezoey hasezoey deleted the vkarpov15/gh-14521 branch May 6, 2024 16:13
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

Successfully merging this pull request may close these issues.

mongoose.set('translateAliases', true) Not working in mongoose@8.3.1
2 participants