Skip to content

Commit fc53a72

Browse files
nbbeekenljhaywar
authored andcommitted
fix: Use getters to sync BulkWriteResult wrappers (#2716)
Adds getters so that mergeBatchResults changes are reflected in wrapper classes. NODE-3055
1 parent 419478f commit fc53a72

File tree

2 files changed

+108
-59
lines changed

2 files changed

+108
-59
lines changed

src/bulk/common.ts

Lines changed: 64 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -165,50 +165,51 @@ export class Batch<T = Document> {
165165
export class BulkWriteResult {
166166
result: BulkResult;
167167

168-
/** Indicates whether this write result was acknowledged. If not, then all other members of this result will be undefined */
169-
// acknowledged: Boolean;
170-
/** Number of documents inserted. */
171-
insertedCount: number;
172-
/** Number of documents matched for update. */
173-
matchedCount: number;
174-
/** Number of documents modified. */
175-
modifiedCount: number;
176-
/** Number of documents deleted. */
177-
deletedCount: number;
178-
/** Number of documents upserted. */
179-
upsertedCount: number;
180-
/** Inserted document generated Id's, hash key is the index of the originating operation */
181-
insertedIds: { [key: number]: ObjectId };
182-
/** Upserted document generated Id's, hash key is the index of the originating operation */
183-
upsertedIds: { [key: number]: ObjectId };
184-
185168
/**
186169
* Create a new BulkWriteResult instance
187170
* @internal
188171
*/
189172
constructor(bulkResult: BulkResult) {
190173
this.result = bulkResult;
191-
this.insertedCount = bulkResult.nInserted;
192-
this.matchedCount = bulkResult.nMatched;
193-
this.modifiedCount = bulkResult.nModified || 0;
194-
this.deletedCount = bulkResult.nRemoved;
195-
this.upsertedCount = bulkResult.upserted.length;
196-
this.upsertedIds = {};
197-
this.insertedIds = {};
198-
199-
// Inserted documents
200-
const inserted = bulkResult.insertedIds;
201-
// Map inserted ids
202-
for (let i = 0; i < inserted.length; i++) {
203-
this.insertedIds[inserted[i].index] = inserted[i]._id;
174+
}
175+
176+
/** Number of documents inserted. */
177+
get insertedCount(): number {
178+
return this.result.nInserted ?? 0;
179+
}
180+
/** Number of documents matched for update. */
181+
get matchedCount(): number {
182+
return this.result.nMatched ?? 0;
183+
}
184+
/** Number of documents modified. */
185+
get modifiedCount(): number {
186+
return this.result.nModified ?? 0;
187+
}
188+
/** Number of documents deleted. */
189+
get deletedCount(): number {
190+
return this.result.nRemoved ?? 0;
191+
}
192+
/** Number of documents upserted. */
193+
get upsertedCount(): number {
194+
return this.result.upserted.length ?? 0;
195+
}
196+
197+
/** Upserted document generated Id's, hash key is the index of the originating operation */
198+
get upsertedIds(): { [key: number]: any } {
199+
const upserted: { [index: number]: any } = {};
200+
for (const doc of this.result.upserted ?? []) {
201+
upserted[doc.index] = doc._id;
204202
}
203+
return upserted;
204+
}
205205

206-
// Upserted documents
207-
const upserted = bulkResult.upserted;
208-
// Map upserted ids
209-
for (let i = 0; i < upserted.length; i++) {
210-
this.upsertedIds[upserted[i].index] = upserted[i]._id;
206+
/** Inserted document generated Id's, hash key is the index of the originating operation */
207+
get insertedIds(): { [key: number]: any } {
208+
const inserted: { [index: number]: any } = {};
209+
for (const doc of this.result.insertedIds ?? []) {
210+
inserted[doc.index] = doc._id;
211211
}
212+
return inserted;
212213
}
213214

214215
/** Evaluates to true if the bulk operation correctly executes */
@@ -674,36 +675,42 @@ function handleMongoWriteConcernError(
674675
export class MongoBulkWriteError extends MongoError {
675676
result: BulkWriteResult;
676677

677-
/** Number of documents inserted. */
678-
insertedCount: number;
679-
/** Number of documents matched for update. */
680-
matchedCount: number;
681-
/** Number of documents modified. */
682-
modifiedCount: number;
683-
/** Number of documents deleted. */
684-
deletedCount: number;
685-
/** Number of documents upserted. */
686-
upsertedCount: number;
687-
/** Inserted document generated Id's, hash key is the index of the originating operation */
688-
insertedIds: { [key: number]: ObjectId };
689-
/** Upserted document generated Id's, hash key is the index of the originating operation */
690-
upsertedIds: { [key: number]: ObjectId };
691-
692678
/** Creates a new MongoBulkWriteError */
693679
constructor(error: AnyError, result: BulkWriteResult) {
694680
super(error as Error);
695681
Object.assign(this, error);
696682

697683
this.name = 'MongoBulkWriteError';
698684
this.result = result;
685+
}
699686

700-
this.insertedCount = result.insertedCount;
701-
this.matchedCount = result.matchedCount;
702-
this.modifiedCount = result.modifiedCount;
703-
this.deletedCount = result.deletedCount;
704-
this.upsertedCount = result.upsertedCount;
705-
this.insertedIds = result.insertedIds;
706-
this.upsertedIds = result.upsertedIds;
687+
/** Number of documents inserted. */
688+
get insertedCount(): number {
689+
return this.result.insertedCount;
690+
}
691+
/** Number of documents matched for update. */
692+
get matchedCount(): number {
693+
return this.result.matchedCount;
694+
}
695+
/** Number of documents modified. */
696+
get modifiedCount(): number {
697+
return this.result.modifiedCount;
698+
}
699+
/** Number of documents deleted. */
700+
get deletedCount(): number {
701+
return this.result.deletedCount;
702+
}
703+
/** Number of documents upserted. */
704+
get upsertedCount(): number {
705+
return this.result.upsertedCount;
706+
}
707+
/** Inserted document generated Id's, hash key is the index of the originating operation */
708+
get insertedIds(): { [key: number]: any } {
709+
return this.result.insertedIds;
710+
}
711+
/** Upserted document generated Id's, hash key is the index of the originating operation */
712+
get upsertedIds(): { [key: number]: any } {
713+
return this.result.upsertedIds;
707714
}
708715
}
709716

test/functional/insert.test.js

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
const { assert: test, withClient } = require('./shared');
2+
const { assert: test, withClient, ignoreNsNotFound } = require('./shared');
33
const { setupDatabase } = require('./shared');
44
const Script = require('vm');
55
const { expect } = require('chai');
@@ -15,7 +15,8 @@ const {
1515
Binary,
1616
MinKey,
1717
MaxKey,
18-
Code
18+
Code,
19+
MongoBulkWriteError
1920
} = require('../../src');
2021

2122
/**
@@ -2583,4 +2584,45 @@ describe('Insert', function () {
25832584
});
25842585
}
25852586
});
2587+
2588+
it('MongoBulkWriteError and BulkWriteResult should respect BulkWrite', function () {
2589+
const client = this.configuration.newClient();
2590+
return client
2591+
.connect()
2592+
.then(() => {
2593+
return client.db().collection('test_insertMany_bulkResult').drop();
2594+
})
2595+
.catch(ignoreNsNotFound)
2596+
.then(() => {
2597+
const collection = client.db().collection('test_insertMany_bulkResult');
2598+
return collection.insertMany(
2599+
[
2600+
{ _id: 2, x: 22 },
2601+
{ _id: 2, x: 22 },
2602+
{ _id: 3, x: 33 }
2603+
],
2604+
{ ordered: false }
2605+
);
2606+
})
2607+
.then(() => {
2608+
expect.fail('InsertMany should fail with multi key error');
2609+
})
2610+
.catch(error => {
2611+
expect(error).to.be.instanceOf(MongoBulkWriteError);
2612+
expect(
2613+
error.insertedCount,
2614+
'MongoBulkWriteError.insertedCount did not respect BulkResult.nInserted'
2615+
).to.equal(error.result.result.nInserted);
2616+
expect(
2617+
error.result.insertedCount,
2618+
'BulkWriteResult.insertedCount did not respect BulkResult.nInserted'
2619+
).to.equal(error.result.result.nInserted);
2620+
expect(
2621+
error.result.result.nInserted,
2622+
'BulkWrite did not correctly represent the operation'
2623+
).to.equal(2);
2624+
})
2625+
.finally(() => client.db().collection('test_insertMany_bulkResult').drop())
2626+
.finally(() => client.close());
2627+
});
25862628
});

0 commit comments

Comments
 (0)