@@ -195,18 +195,36 @@ export class BulkWriteResult {
195195 * Create a new BulkWriteResult instance
196196 * @internal
197197 */
198- constructor ( bulkResult : BulkResult ) {
198+ constructor ( bulkResult : BulkResult , isOrdered : boolean ) {
199199 this . result = bulkResult ;
200200 this . insertedCount = this . result . nInserted ?? 0 ;
201201 this . matchedCount = this . result . nMatched ?? 0 ;
202202 this . modifiedCount = this . result . nModified ?? 0 ;
203203 this . deletedCount = this . result . nRemoved ?? 0 ;
204204 this . upsertedCount = this . result . upserted . length ?? 0 ;
205205 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+ ) ;
207209 Object . defineProperty ( this , 'result' , { value : this . result , enumerable : false } ) ;
208210 }
209211
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+
210228 /** Evaluates to true if the bulk operation correctly executes */
211229 get ok ( ) : number {
212230 return this . result . ok ;
@@ -533,7 +551,10 @@ function executeCommands(
533551 callback : Callback < BulkWriteResult >
534552) {
535553 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+ ) ;
537558 }
538559
539560 const batch = bulkOperation . s . batches . shift ( ) as Batch ;
@@ -542,17 +563,26 @@ function executeCommands(
542563 // Error is a driver related error not a bulk op error, return early
543564 if ( err && 'message' in err && ! ( err instanceof MongoWriteConcernError ) ) {
544565 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+ )
546570 ) ;
547571 }
548572
549573 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+ ) ;
551581 }
552582
553583 // Merge the results together
554584 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 ) ;
556586 if ( bulkOperation . handleWriteError ( callback , writeResult ) ) return ;
557587
558588 // Execute the next command in line
@@ -626,6 +656,7 @@ function executeCommands(
626656function handleMongoWriteConcernError (
627657 batch : Batch ,
628658 bulkResult : BulkResult ,
659+ isOrdered : boolean ,
629660 err : MongoWriteConcernError ,
630661 callback : Callback < BulkWriteResult >
631662) {
@@ -637,7 +668,7 @@ function handleMongoWriteConcernError(
637668 message : err . result ?. writeConcernError . errmsg ,
638669 code : err . result ?. writeConcernError . result
639670 } ,
640- new BulkWriteResult ( bulkResult )
671+ new BulkWriteResult ( bulkResult , isOrdered )
641672 )
642673 ) ;
643674}
0 commit comments