Skip to content

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
@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 [email protected]

2 participants