-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Comments
For item (2), |
Thanks for the quick turnaround there! That definitely solves the biggest pieces of the issue here. The only other remaining piece is making it easier to access the i-th WriteError if several of the ops got filtered out due to having validation issues. It should be possible to do this now on my end (e.g. by counting how many validation issues occurred before the i-th op, and checking |
fix(model+connection): return MongoDB BulkWriteResult instance even if no valid ops
We will look into adding write errors into |
feat(model): make bulkWrite `results` include MongoDB bulk write errors as well as validation errors
Prerequisites
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.
a. The returned object is not truly an instance of
BulkWriteResult
as the types seem to indicate, and thus is missing functions likegetWriteErrorAt(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)
const res = db.bulkWrite([opThatFailsValidation, opThatPassesValidation, opThatFailsValidation], {ordered: false})
and want to find the result of each op,res.getWriteErrorAt(opIdx)
andres.mongoose.validationErrors
both will not work correctly (res.getWriteErrorAt
only contains ops that validated successfully, andres.mongoose.validationErrors
only contains ops that failed).res.mongoose.results
sort of helps with the latter case, but isn't exposed in the typesI'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})
ordb.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)
The text was updated successfully, but these errors were encountered: