diff --git a/lib/helpers/model/castBulkWrite.js b/lib/helpers/model/castBulkWrite.js index 8e47afce706..619281285e9 100644 --- a/lib/helpers/model/castBulkWrite.js +++ b/lib/helpers/model/castBulkWrite.js @@ -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; @@ -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; diff --git a/lib/helpers/query/castUpdate.js b/lib/helpers/query/castUpdate.js index 327d9d2f862..6009852fdd8 100644 --- a/lib/helpers/query/castUpdate.js +++ b/lib/helpers/query/castUpdate.js @@ -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; };