Skip to content

Commit

Permalink
fix: alternative fix for allowing empty update on upsert
Browse files Browse the repository at this point in the history
Fix #9188
  • Loading branch information
vkarpov15 committed Aug 14, 2020
1 parent bd455c4 commit 7646d9e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/helpers/model/castBulkWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ module.exports = function castBulkWrite(originalModel, op, options) {
strict: strict,
overwrite: false,
upsert: op['updateOne'].upsert
});
}, model, op['updateOne']['filter']);
} catch (error) {
return callback(error, null);
}
Expand Down Expand Up @@ -121,7 +121,7 @@ module.exports = function castBulkWrite(originalModel, op, options) {
strict: strict,
overwrite: false,
upsert: op['updateMany'].upsert
});
}, model, op['updateMany']['filter']);

} catch (error) {
return callback(error, null);
Expand Down
6 changes: 4 additions & 2 deletions lib/helpers/query/castUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@ module.exports = function castUpdate(schema, obj, options, context, filter) {
}
}

if (Object.keys(ret).length === 0 && options.upsert) {
if (Object.keys(ret).length === 0 &&
options.upsert &&
Object.keys(filter).length > 0) {
// Trick the driver into allowing empty upserts to work around
// https://github.com/mongodb/node-mongodb-native/pull/2490
return { $fake: true, toBSON: () => ({}) };
return { $setOnInsert: filter };
}

return ret;
Expand Down

0 comments on commit 7646d9e

Please sign in to comment.