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(model): ensure consistent ordering of validation errors in insertMany() with ordered: false and rawResult: true #12866

Merged
merged 3 commits into from Jan 6, 2023

Conversation

vkarpov15
Copy link
Collaborator

Re: #12791

Summary

#12791 points out a few issues with our error reporting with create() and insertMany(). We do have a workaround using insertMany(), but the workaround does have a potential flaw: currently, res.mongoose.validationErrors can report validation errors in any order.

Specifically, the validation errors are in the order that the errors occur, not in the order of the original documents passed in. So if doc1 and doc2 both fail validation, const res = await Model.insertMany([doc1, doc2], { ordered: false, rawResult: true }) can end up with doc2's validation error before doc1's in res.mongoose.validationErrors if doc1's validation takes longer than doc2's.

With this change, res.mongoose.validationErrors will always be in the same order as the documents passed in. So doc1's validation error will always be before doc2's. So to check whether a document at index i was actually inserted, you need to check if i in res.insertedIds. To check if there was a MongoDB-specific write error, check if !(i in res.insertedIds)

Examples

…rawResult: true with mixed validation error and write error re: #12791
Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vkarpov15 vkarpov15 merged commit b7816cd into master Jan 6, 2023
@vkarpov15 vkarpov15 deleted the vkarpov15/gh-12791 branch January 6, 2023 16:55
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.

None yet

2 participants