Skip to content

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

@vkarpov15 vkarpov15 requested a review from hasezoey January 3, 2023 16:34
…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.

3 participants