Skip to content

Commit 436d340

Browse files
committed
fix(bulkWrite): always count undefined values in bson size for bulk
Bulk Writes use bson.calculateObjectSize to determine the size of batches. However, it was not respecting user or default option values like ignoreUndefined, which lead to bulkWrite errors in some edge cases. To be safe, it now always sets ignoreUndefined to false. Fixes NODE-1898
1 parent 73ef728 commit 436d340

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/bulk/ordered.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ const isPromiseLike = require('../utils').isPromiseLike;
2525
function addToOperationsList(bulkOperation, docType, document) {
2626
// Get the bsonSize
2727
const bsonSize = bson.calculateObjectSize(document, {
28-
checkKeys: false
28+
checkKeys: false,
29+
30+
// Since we don't know what the user selected for BSON options here,
31+
// err on the safe side, and check the size with ignoreUndefined: false.
32+
ignoreUndefined: false
2933
});
3034

3135
// Throw error if the doc is bigger than the max BSON size

lib/bulk/unordered.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ const isPromiseLike = require('../utils').isPromiseLike;
2525
function addToOperationsList(bulkOperation, docType, document) {
2626
// Get the bsonSize
2727
const bsonSize = bson.calculateObjectSize(document, {
28-
checkKeys: false
28+
checkKeys: false,
29+
30+
// Since we don't know what the user selected for BSON options here,
31+
// err on the safe side, and check the size with ignoreUndefined: false.
32+
ignoreUndefined: false
2933
});
3034
// Throw error if the doc is bigger than the max BSON size
3135
if (bsonSize >= bulkOperation.s.maxBatchSizeBytes)

0 commit comments

Comments
 (0)