Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/helpers/getDefaultBulkwriteResult.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
function getDefaultBulkwriteResult() {
return {
result: {
ok: 1,
writeErrors: [],
writeConcernErrors: [],
insertedIds: [],
nInserted: 0,
nUpserted: 0,
nMatched: 0,
nModified: 0,
nRemoved: 0,
upserted: []
},
insertedCount: 0,
matchedCount: 0,
modifiedCount: 0,
deletedCount: 0,
upsertedCount: 0,
upsertedIds: {},
insertedIds: {},
n: 0
};
}

module.exports = getDefaultBulkwriteResult;
6 changes: 5 additions & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const applyStatics = require('./helpers/model/applyStatics');
const applyWriteConcern = require('./helpers/schema/applyWriteConcern');
const assignVals = require('./helpers/populate/assignVals');
const castBulkWrite = require('./helpers/model/castBulkWrite');
const getDefaultBulkwriteResult = require('./helpers/getDefaultBulkwriteResult');
const discriminator = require('./helpers/model/discriminator');
const each = require('./helpers/each');
const getDiscriminatorByValue = require('./helpers/discriminator/getDiscriminatorByValue');
Expand Down Expand Up @@ -3493,14 +3494,17 @@ Model.bulkWrite = function(ops, options, callback) {
const validations = ops.map(op => castBulkWrite(this, op, options));

callback = this.$handleCallbackError(callback);

return promiseOrCallback(callback, cb => {
cb = this.$wrapCallback(cb);
each(validations, (fn, cb) => fn(cb), error => {
if (error) {
return cb(error);
}

if (ops.length === 0) {
return cb(null, getDefaultBulkwriteResult());
}

this.collection.bulkWrite(ops, options, (error, res) => {
if (error) {
return cb(error);
Expand Down
36 changes: 35 additions & 1 deletion test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6799,10 +6799,44 @@ describe('Model', function() {
assert.equal(typeof users[0].updatedAt, 'number');
assert.equal(typeof users[1].updatedAt, 'number');

// not-lean queries casts to number even if stored on DB as a date
// not-lean queries cast to number even if stored on DB as a date
assert.equal(users[0] instanceof User, false);
assert.equal(users[1] instanceof User, false);
});
});

it('Model#bulkWrite(...) does not throw an error when provided an empty array (gh-9131)', function() {
return co(function*() {
const userSchema = new Schema();
const User = db.model('User', userSchema);

const res = yield User.bulkWrite([]);

assert.deepEqual(
res,
{
result: {
ok: 1,
writeErrors: [],
writeConcernErrors: [],
insertedIds: [],
nInserted: 0,
nUpserted: 0,
nMatched: 0,
nModified: 0,
nRemoved: 0,
upserted: []
},
insertedCount: 0,
matchedCount: 0,
modifiedCount: 0,
deletedCount: 0,
upsertedCount: 0,
upsertedIds: {},
insertedIds: {},
n: 0
}
);
});
});
});