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

feat(model): add throwOnValidationError option for opting into getting MongooseBulkWriteError if all valid operations succeed in bulkWrite() and insertMany() #13410

Merged
merged 2 commits into from May 19, 2023

Conversation

vkarpov15
Copy link
Collaborator

Fix #13256
Expands on #13258 .

Summary

#13256 is a bit of a strange edge case: currently, if you use bulkWrite() or insertMany() with ordered: false, and there are validation errors but all valid ops succeed, you don't get an error. That's because we return the raw MongoDB server result, decorated with some extra Mongoose properties. This is very inconsistent, because you would expect to get an error if one of the operations failed validation.

This PR adds a new MongooseBulkWriteError class that consolidates some of the patterns we like for reporting bulk write errors: namely, list of validationErrors and a results array that helps you tell what happened with each individual operation or document you passed in.

Examples

…g MongooseBulkWriteError if all valid operations succeed in `bulkWrite()` and `insertMany()`

Fix #13256
@@ -2962,6 +2963,7 @@ Model.startSession = function() {
* @param {Boolean} [options.lean=false] if `true`, skips hydrating and validating the documents. This option is useful if you need the extra performance, but Mongoose won't validate the documents before inserting.
* @param {Number} [options.limit=null] this limits the number of documents being processed (validation/casting) by mongoose in parallel, this does **NOT** send the documents in batches to MongoDB. Use this option if you're processing a large number of documents and your app is running out of memory.
* @param {String|Object|Array} [options.populate=null] populates the result documents. This option is a no-op if `rawResult` is set.
* @param {Boolean} [options.throwOnValidationError=false] If true and `ordered: false`, throw an error if one of the operations failed validation, but all valid operations completed successfully.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd argue that this should default to true, since this is the intuitive behavior anyway, we can consider this as a bug rather than a missing feature. People should opt-out if they want backward-compatible behavior for some reason, then on v8.0 we can remove the option altogether.

What do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The problem is that we have tests that assert on the counterintuitive behavior, and for stability's sake I like to avoid changing existing tests outside of major releases.

Copy link
Collaborator

Choose a reason for hiding this comment

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

In that case I think it's good to merge as is, and remove the option in v8.0.

What do you think? Or would you rather to keep it as an option in v8, and maybe just change the default?

lib/model.js Outdated Show resolved Hide resolved
Copy link
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez left a comment

Choose a reason for hiding this comment

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

LGTM, thanks! 👍

@vkarpov15 vkarpov15 merged commit 8b41e8a into 7.2 May 19, 2023
37 checks passed
@hasezoey hasezoey deleted the vkarpov15/gh-13256 branch May 19, 2023 16:48
vkarpov15 added a commit that referenced this pull request May 16, 2024
…g MongooseBulkWriteError if all valid operations succeed in bulkWrite() and insertMany()

Backport #13410
Backport #14587
Fix #14572
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

3 participants