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