Skip to content

Commit

Permalink
fix(model): set isNew to false for documents that were successfully…
Browse files Browse the repository at this point in the history
… inserted by `insertMany` with `ordered = false` when an error occurred

Fix #9677
  • Loading branch information
vkarpov15 committed Dec 10, 2020
1 parent b2da840 commit 1be4d87
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3332,9 +3332,13 @@ Model.$__insertMany = function(arr, options, callback) {

// `insertedDocs` is a Mongoose-specific property
const erroredIndexes = new Set(get(error, 'writeErrors', []).map(err => err.index));
error.insertedDocs = docAttributes.filter((doc, i) => {
return !erroredIndexes.has(i);
});
error.insertedDocs = docAttributes.
filter((doc, i) => !erroredIndexes.has(i)).
map(function setIsNewForInsertedDoc(doc) {
doc.$__reset();
_setIsNew(doc, false);
return doc;
});

callback(error, null);
return;
Expand Down

0 comments on commit 1be4d87

Please sign in to comment.