@@ -479,7 +479,21 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
479
479
) ;
480
480
}
481
481
482
- let document ;
482
+ // If `documentsReturnedIn` not set or raw is not enabled, use input bson options
483
+ // Otherwise, support raw flag. Raw only works for cursors that hardcode firstBatch/nextBatch fields
484
+ const bsonOptions =
485
+ options . documentsReturnedIn == null || ! options . raw
486
+ ? options
487
+ : {
488
+ ...options ,
489
+ raw : false ,
490
+ fieldsAsRaw : { [ options . documentsReturnedIn ] : true }
491
+ } ;
492
+
493
+ /** MongoDBResponse instance or subclass */
494
+ let document : MongoDBResponse | undefined = undefined ;
495
+ /** Cached result of a toObject call */
496
+ let object : Document | undefined = undefined ;
483
497
try {
484
498
this . throwIfAborted ( ) ;
485
499
for await ( document of this . sendWire ( message , options , responseType ) ) {
@@ -493,15 +507,12 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
493
507
}
494
508
495
509
if ( document . has ( 'writeConcernError' ) ) {
496
- const objectWithWriteConcernError = document . toObject ( options ) ;
497
- throw new MongoWriteConcernError (
498
- objectWithWriteConcernError . writeConcernError ,
499
- objectWithWriteConcernError
500
- ) ;
510
+ object ??= document . toObject ( bsonOptions ) ;
511
+ throw new MongoWriteConcernError ( object . writeConcernError , object ) ;
501
512
}
502
513
503
514
if ( document . isError ) {
504
- throw new MongoServerError ( document . toObject ( options ) ) ;
515
+ throw new MongoServerError ( ( object ??= document . toObject ( bsonOptions ) ) ) ;
505
516
}
506
517
507
518
if ( this . shouldEmitAndLogCommand ) {
@@ -513,25 +524,15 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
513
524
new CommandSucceededEvent (
514
525
this ,
515
526
message ,
516
- options . noResponse ? undefined : document . toObject ( options ) ,
527
+ options . noResponse ? undefined : ( object ??= document . toObject ( bsonOptions ) ) ,
517
528
started ,
518
529
this . description . serverConnectionId
519
530
)
520
531
) ;
521
532
}
522
533
523
534
if ( responseType == null ) {
524
- // If `documentsReturnedIn` not set or raw is not enabled, use input bson options
525
- // Otherwise, support raw flag. Raw only works for cursors that hardcode firstBatch/nextBatch fields
526
- const bsonOptions =
527
- options . documentsReturnedIn == null || ! options . raw
528
- ? options
529
- : {
530
- ...options ,
531
- raw : false ,
532
- fieldsAsRaw : { [ options . documentsReturnedIn ] : true }
533
- } ;
534
- yield document . toObject ( bsonOptions ) ;
535
+ yield ( object ??= document . toObject ( bsonOptions ) ) ;
535
536
} else {
536
537
yield document ;
537
538
}
@@ -549,7 +550,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
549
550
new CommandSucceededEvent (
550
551
this ,
551
552
message ,
552
- options . noResponse ? undefined : document ?. toObject ( options ) ,
553
+ options . noResponse ? undefined : ( object ??= document ?. toObject ( bsonOptions ) ) ,
553
554
started ,
554
555
this . description . serverConnectionId
555
556
)
0 commit comments