Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #8331 #8332

Merged
merged 2 commits into from Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/helpers/model/castBulkWrite.js
Expand Up @@ -34,6 +34,9 @@ module.exports = function castBulkWrite(model, op, options) {
op = op['updateOne'];
return (callback) => {
try {
if (!op['filter']) throw new Error('Must provide a filter object.');
if (!op['update']) throw new Error('Must provide an update object.');

op['filter'] = cast(model.schema, op['filter']);
op['update'] = castUpdate(model.schema, op['update'], {
strict: model.schema.options.strict,
Expand Down Expand Up @@ -61,6 +64,9 @@ module.exports = function castBulkWrite(model, op, options) {
op = op['updateMany'];
return (callback) => {
try {
if (!op['filter']) throw new Error('Must provide a filter object.');
if (!op['update']) throw new Error('Must provide an update object.');

op['filter'] = cast(model.schema, op['filter']);
op['update'] = castUpdate(model.schema, op['update'], {
strict: model.schema.options.strict,
Expand Down
28 changes: 28 additions & 0 deletions test/model.test.js
Expand Up @@ -5671,6 +5671,34 @@ describe('Model', function() {
then(() => Model.findOne()).
then(doc => assert.equal(doc.nested.name, 'foo'));
});

it('throws an error if no update object is provided (gh-8331)', function() {
const userSchema = new Schema({ name: { type: String, required: true } });
const User = db.model('gh8331', userSchema);

return co(function*() {
const createdUser = yield User.create({ name: 'Hafez' });
let threw = false;
try {
yield User.bulkWrite([{
updateOne: {
filter: { _id: createdUser._id }
}
}]);
}
catch (err) {
threw = true;
assert.equal(err.message, 'Must provide an update object.');
}
finally {
assert.equal(threw, true);

const userAfterUpdate = yield User.findOne({ _id: createdUser._id });

assert.equal(userAfterUpdate.name, 'Hafez', 'Document data is not wiped if no update object is provided.');
}
});
});
});

it('insertMany with Decimal (gh-5190)', function(done) {
Expand Down