Skip to content

Commit

Permalink
fix(update): allow upsert with empty updates
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Aug 14, 2020
1 parent d374f14 commit bd455c4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/helpers/model/castBulkWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ module.exports = function castBulkWrite(originalModel, op, options) {
} else if (op['updateOne']) {
return (callback) => {
try {
if (!op['updateOne']['filter']) throw new Error('Must provide a filter object.');
if (!op['updateOne']['update']) throw new Error('Must provide an update object.');
if (!op['updateOne']['filter']) {
throw new Error('Must provide a filter object.');
}
if (!op['updateOne']['update']) {
throw new Error('Must provide an update object.');
}

const model = decideModelByObject(originalModel, op['updateOne']['filter']);
const schema = model.schema;
Expand Down Expand Up @@ -80,8 +84,12 @@ module.exports = function castBulkWrite(originalModel, op, options) {
} else if (op['updateMany']) {
return (callback) => {
try {
if (!op['updateMany']['filter']) throw new Error('Must provide a filter object.');
if (!op['updateMany']['update']) throw new Error('Must provide an update object.');
if (!op['updateMany']['filter']) {
throw new Error('Must provide a filter object.');
}
if (!op['updateMany']['update']) {
throw new Error('Must provide an update object.');
}

const model = decideModelByObject(originalModel, op['updateMany']['filter']);
const schema = model.schema;
Expand Down
6 changes: 6 additions & 0 deletions lib/helpers/query/castUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ module.exports = function castUpdate(schema, obj, options, context, filter) {
}
}

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

return ret;
};

Expand Down

0 comments on commit bd455c4

Please sign in to comment.