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

Several issues with bulkWrite API exposing validation errors #15265

Open
2 tasks done
alex-statsig opened this issue Feb 19, 2025 · 1 comment
Open
2 tasks done

Several issues with bulkWrite API exposing validation errors #15265

alex-statsig opened this issue Feb 19, 2025 · 1 comment
Milestone

Comments

@alex-statsig
Copy link
Contributor

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

7.8.4

Node.js version

23.x

MongoDB server version

8.x

Typescript version (if applicable)

No response

Description

There are a few issues I've noticed with the bulkWrite API with ordered=false w.r.t. exposing write/validation errors where the returned object doesn't meet the expected types or doesn't comply with expected contracts.

  1. If db.bulkWrite() is called with ordered=false and none of the ops pass mongoose schema validation:
    a. The returned object is not truly an instance of BulkWriteResult as the types seem to indicate, and thus is missing functions like getWriteErrorAt(index) (code pointer)
    b. The returned object does not expose the validation errors (even though it does expose them if at least one passes validation). This feels inconsistent, the validationErrors return type is only helpful if consistently provided. As-is, it returns a payload that deceptively indicates the request worked (nModified is 0, but that is a legitimately possible result of a successful write)
  2. When some operations provided to bulkWrite fail validation and others pass, it becomes impossible to distinguish which returned errors match to which input op. Typically for a BulkWriteResult, res.getWriteErrorAt(index) can be used to see exactly which op had an error. However, with mongoose the result has already filtered out invalid objects. So if you call const res = db.bulkWrite([opThatFailsValidation, opThatPassesValidation, opThatFailsValidation], {ordered: false}) and want to find the result of each op, res.getWriteErrorAt(opIdx) and res.mongoose.validationErrors both will not work correctly (res.getWriteErrorAt only contains ops that validated successfully, and res.mongoose.validationErrors only contains ops that failed). res.mongoose.results sort of helps with the latter case, but isn't exposed in the types

I've lumped these into one issue since they're all somewhat related, but happy to split them up too and/or help put up fixes if the issue makes sense

Note: This seems somewhat related to #14572, though the solution there only helps in the "throw" handling

Steps to Reproduce

Steps are mostly outlined above, but generally calling db.bulkWrite([opThatFailsValidation, opThatPassesValidation, opThatFailsValidation], {ordered: false}) or db.bulkWrite([opThatFailsValidation], {ordered: false})

Expected Behavior

A BulkWriteError instance should be returned consistently from the bulkWrite method, with res.getWriteErrorAt(idx) consistently returning the error from the idx-th op passed to bulkWrite. Ideally there is also a way to grab the validation error for the idx-th op passed to bulkWrite (i.e. what res.mongoose.results seems to be, just needs to be added to types)

@vkarpov15 vkarpov15 added this to the 8.10.2 milestone Feb 20, 2025
@vkarpov15
Copy link
Collaborator

For item (2), res.mongoose.results is the way to go. res.mongoose.results[i] will contain whatever error occurred for operation at index i, or null if no error occurred. We added that to types in #15266, along with a fix for (1).

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

No branches or pull requests

2 participants