@@ -165,50 +165,51 @@ export class Batch<T = Document> {
165
165
export class BulkWriteResult {
166
166
result : BulkResult ;
167
167
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
-
185
168
/**
186
169
* Create a new BulkWriteResult instance
187
170
* @internal
188
171
*/
189
172
constructor ( bulkResult : BulkResult ) {
190
173
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 ;
204
202
}
203
+ return upserted ;
204
+ }
205
205
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 ;
211
211
}
212
+ return inserted ;
212
213
}
213
214
214
215
/** Evaluates to true if the bulk operation correctly executes */
@@ -674,36 +675,42 @@ function handleMongoWriteConcernError(
674
675
export class MongoBulkWriteError extends MongoError {
675
676
result : BulkWriteResult ;
676
677
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
-
692
678
/** Creates a new MongoBulkWriteError */
693
679
constructor ( error : AnyError , result : BulkWriteResult ) {
694
680
super ( error as Error ) ;
695
681
Object . assign ( this , error ) ;
696
682
697
683
this . name = 'MongoBulkWriteError' ;
698
684
this . result = result ;
685
+ }
699
686
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 ;
707
714
}
708
715
}
709
716
0 commit comments