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
And it's hard to describe in few sentences, what is wrong, because it seems to me that error handling is messed up.
For example, if my data is: [{"code":"MEDIUM","text":"1111"},{"code":"test","text":"222"},{"code":"HARD","text":"2222"}]
Response is: {"error":{"title":"BulkWriteError","code":11000,"message":"E11000 duplicate key error collection: game.questiontypes index: code_1 dup key: { code: \"MEDIUM\" }"},"writeErrors":[{"code":11000,"index":0,"errmsg":"E11000 duplicate key error collection: game.questiontypes index: code_1 dup key: { code: \"MEDIUM\" }","op":{"questions":[],"_id":"5eb19d28e646883e3459ce07","code":"MEDIUM","text":"1111","__v":0,"createdAt":"2020-05-05T17:06:48.409Z","updatedAt":"2020-05-05T17:06:48.409Z"}},{"code":11000,"index":2,"errmsg":"E11000 duplicate key error collection: game.questiontypes index: code_1 dup key: { code: \"HARD\" }","op":{"questions":[],"_id":"5eb19d28e646883e3459ce09","code":"HARD","text":"2222","__v":0,"createdAt":"2020-05-05T17:06:48.410Z","updatedAt":"2020-05-05T17:06:48.410Z"}}]}
Documents with MEDIUM and HARD code types exist, I will get errors for them, but I don't have any information about insterting test - no error, no data in docs callback parameter, but that one document was actually inserted.
Another option of strange behavior:
if my data is [{"code":"MEDIUM","text":"1111"},{"code":"test2","text":""}, I will get only one error - {"error":{"title":"BulkWriteError","code":11000,"message":"E11000 duplicate key error collection: game.questiontypes index: code_1 dup key: { code: \"MEDIUM\" }"}} though I expect for at least two errors - no text parameter for second array value was provided.
It looks like required fields validation runs only if every duplicate validation is correct
created documents aren't passed to the callback function if any 'unique' validation error has appeared for array of inserting documents.
WriteErrors appear on error object if number of errors is more than 1, though it would be nicer to place it if it's only one error either
If the current behavior is a bug, please provide the steps to reproduce.
Try to create next combinations:
a) 1 duplicate, at least 1 new doc without required fields
b) 2 duplicate, at least 1 new doc without required fields
c) only new docs without required fields
d) 1 duplicate, at least 1 new doc with required fields
Behold the apocalypse for validation & errors!
What is the expected behavior?
Unified error object? At least mongoose validation for required fields should always run and return errors for those documents which passes mongo check for unique validation but not passes other mongoose validations
Return of inserted documents should be correct (not the empty array, when in real case database is +n new valid documents)
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
node: 13.1.0
mongoose: 5.9.11
mongodb: MongoDB 4.2.1 Community
The text was updated successfully, but these errors were encountered:
NebSehemvi
changed the title
No unified behavior for insertMany error handling?
insertMany: problems with errors & inserted documents handling
May 10, 2020
One important note to clarify: unique is not a validator. That has some important ramifications for how insertMany() error handling works: Mongoose executes all validators, and then sends the documents that pass validation to MongoDB, which is responsible for catching duplicates.
There are some improvements we can make here though:
You're write about writeErrors only appearing if there's more than 1 error. That's a problem that we should fix.
The ids of all inserted docs are available in err.result.result.insertedIds, so in order to get the documents that have been inserted, you need to go through that list and filter out any that errored out. We should add a property insertedDocs.
vkarpov15
added
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
and removed
help
This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
labels
May 16, 2020
Do you want to request a feature or report a bug?
Bug, but maybe a feature
What is the current behavior?
I have Mongoose schema:
And I have an API for inserting question types:
And it's hard to describe in few sentences, what is wrong, because it seems to me that error handling is messed up.
For example, if my data is:
[{"code":"MEDIUM","text":"1111"},{"code":"test","text":"222"},{"code":"HARD","text":"2222"}]
Response is:
{"error":{"title":"BulkWriteError","code":11000,"message":"E11000 duplicate key error collection: game.questiontypes index: code_1 dup key: { code: \"MEDIUM\" }"},"writeErrors":[{"code":11000,"index":0,"errmsg":"E11000 duplicate key error collection: game.questiontypes index: code_1 dup key: { code: \"MEDIUM\" }","op":{"questions":[],"_id":"5eb19d28e646883e3459ce07","code":"MEDIUM","text":"1111","__v":0,"createdAt":"2020-05-05T17:06:48.409Z","updatedAt":"2020-05-05T17:06:48.409Z"}},{"code":11000,"index":2,"errmsg":"E11000 duplicate key error collection: game.questiontypes index: code_1 dup key: { code: \"HARD\" }","op":{"questions":[],"_id":"5eb19d28e646883e3459ce09","code":"HARD","text":"2222","__v":0,"createdAt":"2020-05-05T17:06:48.410Z","updatedAt":"2020-05-05T17:06:48.410Z"}}]}
Documents with MEDIUM and HARD code types exist, I will get errors for them, but I don't have any information about insterting test - no error, no data in docs callback parameter, but that one document was actually inserted.
Another option of strange behavior:
if my data is
[{"code":"MEDIUM","text":"1111"},{"code":"test2","text":""}
, I will get only one error -{"error":{"title":"BulkWriteError","code":11000,"message":"E11000 duplicate key error collection: game.questiontypes index: code_1 dup key: { code: \"MEDIUM\" }"}}
though I expect for at least two errors - no text parameter for second array value was provided.If the current behavior is a bug, please provide the steps to reproduce.
a) 1 duplicate, at least 1 new doc without required fields
b) 2 duplicate, at least 1 new doc without required fields
c) only new docs without required fields
d) 1 duplicate, at least 1 new doc with required fields
What is the expected behavior?
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
node: 13.1.0
mongoose: 5.9.11
mongodb: MongoDB 4.2.1 Community
The text was updated successfully, but these errors were encountered: