Skip to content

Commit 82f8055

Browse files
authored
Merge pull request #1489 from mbroadst/bulk-inserted-ids-fix
fix(UnorderedBulkOperation): push correct index for INSERT ops
2 parents 5e3b2ed + e53d02c commit 82f8055

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

lib/bulk/unordered.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ FindOperatorsUnordered.prototype.remove = function() {
148148
var addToOperationsList = function(_self, docType, document) {
149149
// Get the bsonSize
150150
var bsonSize = bson.calculateObjectSize(document, {
151-
checkKeys: false,
151+
checkKeys: false,
152152
});
153153
// Throw error if the doc is bigger than the max BSON size
154154
if(bsonSize >= _self.s.maxBatchSizeBytes) throw toError("document is larger than the maximum size " + _self.s.maxBatchSizeBytes);
@@ -178,18 +178,20 @@ var addToOperationsList = function(_self, docType, document) {
178178
}
179179

180180
// We have an array of documents
181+
var insertIndex;
181182
if(Array.isArray(document)) {
182183
throw toError("operation passed in cannot be an Array");
183184
} else {
184185
_self.s.currentBatch.operations.push(document);
185186
_self.s.currentBatch.originalIndexes.push(_self.s.currentIndex);
187+
insertIndex = _self.s.currentIndex;
186188
_self.s.currentIndex = _self.s.currentIndex + 1;
187189
}
188190

189191
// Save back the current Batch to the right type
190192
if(docType == common.INSERT) {
191193
_self.s.currentInsertBatch = _self.s.currentBatch;
192-
_self.s.bulkResult.insertedIds.push({index: _self.s.currentIndex, _id: document._id});
194+
_self.s.bulkResult.insertedIds.push({index: insertIndex, _id: document._id});
193195
} else if(docType == common.UPDATE) {
194196
_self.s.currentUpdateBatch = _self.s.currentBatch;
195197
} else if(docType == common.REMOVE) {

test/functional/insert_tests.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,6 +2219,40 @@ exports['Correctly allow forceServerObjectId for insertMany'] = {
22192219
}
22202220
}
22212221

2222+
exports['should return correct number of ids for insertMany { ordered: true }'] = {
2223+
metadata: { requires: { topology: [ 'single' ] } },
2224+
test: function(configuration, test) {
2225+
var db = configuration.newDbInstance({ w: 1 }, { poolSize: 1, auto_reconnect: false });
2226+
db.open(function(err, db) {
2227+
test.equal(null, err);
2228+
db.collection('inserted_ids_test').insertMany([{}, {}, {}], { ordered: true })
2229+
.then(function(r) {
2230+
test.equal(null, err);
2231+
test.equal(3, r.insertedIds.length);
2232+
db.close();
2233+
test.done();
2234+
});
2235+
});
2236+
}
2237+
}
2238+
2239+
exports['should return correct number of ids for insertMany { ordered: false }'] = {
2240+
metadata: { requires: { topology: [ 'single' ] } },
2241+
test: function(configuration, test) {
2242+
var db = configuration.newDbInstance({ w: 1 }, { poolSize:1, auto_reconnect:false });
2243+
db.open(function(err, db) {
2244+
test.equal(null, err);
2245+
db.collection('inserted_ids_test').insertMany([{}, {}, {}], { ordered: false })
2246+
.then(function(r) {
2247+
test.equal(null, err);
2248+
test.equal(3, r.insertedIds.length);
2249+
db.close();
2250+
test.done();
2251+
});
2252+
});
2253+
}
2254+
}
2255+
22222256
exports['Insert document including sub documents'] = {
22232257
metadata: { requires: { topology: ['single'] } },
22242258

0 commit comments

Comments
 (0)