@@ -195,18 +195,36 @@ export class BulkWriteResult {
195
195
* Create a new BulkWriteResult instance
196
196
* @internal
197
197
*/
198
- constructor ( bulkResult : BulkResult ) {
198
+ constructor ( bulkResult : BulkResult , isOrdered : boolean ) {
199
199
this . result = bulkResult ;
200
200
this . insertedCount = this . result . nInserted ?? 0 ;
201
201
this . matchedCount = this . result . nMatched ?? 0 ;
202
202
this . modifiedCount = this . result . nModified ?? 0 ;
203
203
this . deletedCount = this . result . nRemoved ?? 0 ;
204
204
this . upsertedCount = this . result . upserted . length ?? 0 ;
205
205
this . upsertedIds = BulkWriteResult . generateIdMap ( this . result . upserted ) ;
206
- this . insertedIds = BulkWriteResult . generateIdMap ( this . result . insertedIds ) ;
206
+ this . insertedIds = BulkWriteResult . generateIdMap (
207
+ this . getSuccessfullyInsertedIds ( bulkResult , isOrdered )
208
+ ) ;
207
209
Object . defineProperty ( this , 'result' , { value : this . result , enumerable : false } ) ;
208
210
}
209
211
212
+ /**
213
+ * Returns document_ids that were actually inserted
214
+ * @internal
215
+ */
216
+ private getSuccessfullyInsertedIds ( bulkResult : BulkResult , isOrdered : boolean ) : Document [ ] {
217
+ if ( bulkResult . writeErrors . length === 0 ) return bulkResult . insertedIds ;
218
+
219
+ if ( isOrdered ) {
220
+ return bulkResult . insertedIds . slice ( 0 , bulkResult . writeErrors [ 0 ] . index ) ;
221
+ }
222
+
223
+ return bulkResult . insertedIds . filter (
224
+ ( { index } ) => ! bulkResult . writeErrors . some ( writeError => index === writeError . index )
225
+ ) ;
226
+ }
227
+
210
228
/** Evaluates to true if the bulk operation correctly executes */
211
229
get ok ( ) : number {
212
230
return this . result . ok ;
@@ -533,7 +551,10 @@ function executeCommands(
533
551
callback : Callback < BulkWriteResult >
534
552
) {
535
553
if ( bulkOperation . s . batches . length === 0 ) {
536
- return callback ( undefined , new BulkWriteResult ( bulkOperation . s . bulkResult ) ) ;
554
+ return callback (
555
+ undefined ,
556
+ new BulkWriteResult ( bulkOperation . s . bulkResult , bulkOperation . isOrdered )
557
+ ) ;
537
558
}
538
559
539
560
const batch = bulkOperation . s . batches . shift ( ) as Batch ;
@@ -542,17 +563,26 @@ function executeCommands(
542
563
// Error is a driver related error not a bulk op error, return early
543
564
if ( err && 'message' in err && ! ( err instanceof MongoWriteConcernError ) ) {
544
565
return callback (
545
- new MongoBulkWriteError ( err , new BulkWriteResult ( bulkOperation . s . bulkResult ) )
566
+ new MongoBulkWriteError (
567
+ err ,
568
+ new BulkWriteResult ( bulkOperation . s . bulkResult , bulkOperation . isOrdered )
569
+ )
546
570
) ;
547
571
}
548
572
549
573
if ( err instanceof MongoWriteConcernError ) {
550
- return handleMongoWriteConcernError ( batch , bulkOperation . s . bulkResult , err , callback ) ;
574
+ return handleMongoWriteConcernError (
575
+ batch ,
576
+ bulkOperation . s . bulkResult ,
577
+ bulkOperation . isOrdered ,
578
+ err ,
579
+ callback
580
+ ) ;
551
581
}
552
582
553
583
// Merge the results together
554
584
mergeBatchResults ( batch , bulkOperation . s . bulkResult , err , result ) ;
555
- const writeResult = new BulkWriteResult ( bulkOperation . s . bulkResult ) ;
585
+ const writeResult = new BulkWriteResult ( bulkOperation . s . bulkResult , bulkOperation . isOrdered ) ;
556
586
if ( bulkOperation . handleWriteError ( callback , writeResult ) ) return ;
557
587
558
588
// Execute the next command in line
@@ -626,6 +656,7 @@ function executeCommands(
626
656
function handleMongoWriteConcernError (
627
657
batch : Batch ,
628
658
bulkResult : BulkResult ,
659
+ isOrdered : boolean ,
629
660
err : MongoWriteConcernError ,
630
661
callback : Callback < BulkWriteResult >
631
662
) {
@@ -637,7 +668,7 @@ function handleMongoWriteConcernError(
637
668
message : err . result ?. writeConcernError . errmsg ,
638
669
code : err . result ?. writeConcernError . result
639
670
} ,
640
- new BulkWriteResult ( bulkResult )
671
+ new BulkWriteResult ( bulkResult , isOrdered )
641
672
)
642
673
) ;
643
674
}
0 commit comments