From 9f4a9c2327c1b9bdebb35c57b1651f1ec4f067f2 Mon Sep 17 00:00:00 2001 From: Yarchik Date: Thu, 2 Jul 2026 12:47:04 +0100 Subject: [PATCH] fix: key unordered bulk insertedIds by originating operation index --- src/bulk/unordered.ts | 2 +- test/unit/bulk.test.ts | 63 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 test/unit/bulk.test.ts diff --git a/src/bulk/unordered.ts b/src/bulk/unordered.ts index 97a134613b0..35add067b3b 100644 --- a/src/bulk/unordered.ts +++ b/src/bulk/unordered.ts @@ -97,7 +97,7 @@ export class UnorderedBulkOperation extends BulkOperationBase { if (batchType === BatchType.INSERT) { this.s.currentInsertBatch = this.s.currentBatch; this.s.bulkResult.insertedIds.push({ - index: this.s.bulkResult.insertedIds.length, + index: this.s.currentIndex - 1, _id: (document as Document)._id }); } else if (batchType === BatchType.UPDATE) { diff --git a/test/unit/bulk.test.ts b/test/unit/bulk.test.ts new file mode 100644 index 00000000000..88d44826593 --- /dev/null +++ b/test/unit/bulk.test.ts @@ -0,0 +1,63 @@ +import { expect } from 'chai'; + +import { + DEFAULT_PK_FACTORY, + MongoDBCollectionNamespace, + OrderedBulkOperation, + UnorderedBulkOperation +} from '../mongodb'; + +describe('Bulk Operation insertedIds', function () { + function makeFakeCollection() { + const topology = { + lastHello() { + return { maxBsonObjectSize: 16 * 1024 * 1024, maxWriteBatchSize: 1000 }; + }, + s: { options: {} } + }; + const collection: any = { + client: { topology }, + topology, + db: { options: {} }, + bsonOptions: {}, + s: { + namespace: new MongoDBCollectionNamespace('test', 'coll'), + pkFactory: DEFAULT_PK_FACTORY, + bsonOptions: {}, + collection: undefined + } + }; + collection.s.collection = collection; + return collection; + } + + function addMixedOperations(bulk: OrderedBulkOperation | UnorderedBulkOperation) { + bulk.raw({ insertOne: { document: { _id: 'a', x: 1 } } }); + bulk.raw({ updateOne: { filter: { x: 1 }, update: { $set: { y: 2 } } } }); + bulk.raw({ insertOne: { document: { _id: 'b', x: 3 } } }); + bulk.raw({ deleteOne: { filter: { x: 3 } } }); + bulk.raw({ insertOne: { document: { _id: 'c', x: 5 } } }); + } + + it('ordered bulk keys insertedIds by the originating operation index', function () { + const bulk = new OrderedBulkOperation(makeFakeCollection(), { ordered: true } as any); + addMixedOperations(bulk); + const insertedIds = (bulk as any).s.bulkResult.insertedIds; + expect(insertedIds).to.deep.equal([ + { index: 0, _id: 'a' }, + { index: 2, _id: 'b' }, + { index: 4, _id: 'c' } + ]); + }); + + it('unordered bulk keys insertedIds by the originating operation index', function () { + const bulk = new UnorderedBulkOperation(makeFakeCollection(), { ordered: false } as any); + addMixedOperations(bulk); + const insertedIds = (bulk as any).s.bulkResult.insertedIds; + expect(insertedIds).to.deep.equal([ + { index: 0, _id: 'a' }, + { index: 2, _id: 'b' }, + { index: 4, _id: 'c' } + ]); + }); +});