You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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)
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)
The text was updated successfully, but these errors were encountered:
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).
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: