@@ -484,22 +484,23 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
484484
485485 command (
486486 ns : MongoDBNamespace ,
487- cmd : Document ,
487+ command : Document ,
488488 options : CommandOptions | undefined ,
489489 callback : Callback
490490 ) : void {
491- const readPreference = getReadPreference ( cmd , options ) ;
491+ let cmd = { ...command } ;
492+
493+ const readPreference = getReadPreference ( options ) ;
492494 const shouldUseOpMsg = supportsOpMsg ( this ) ;
493495 const session = options ?. session ;
494496
495497 let clusterTime = this . clusterTime ;
496- let finalCmd = Object . assign ( { } , cmd ) ;
497498
498499 if ( this . serverApi ) {
499500 const { version, strict, deprecationErrors } = this . serverApi ;
500- finalCmd . apiVersion = version ;
501- if ( strict != null ) finalCmd . apiStrict = strict ;
502- if ( deprecationErrors != null ) finalCmd . apiDeprecationErrors = deprecationErrors ;
501+ cmd . apiVersion = version ;
502+ if ( strict != null ) cmd . apiStrict = strict ;
503+ if ( deprecationErrors != null ) cmd . apiDeprecationErrors = deprecationErrors ;
503504 }
504505
505506 if ( hasSessionSupport ( this ) && session ) {
@@ -511,7 +512,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
511512 clusterTime = session . clusterTime ;
512513 }
513514
514- const err = applySession ( session , finalCmd , options ) ;
515+ const err = applySession ( session , cmd , options ) ;
515516 if ( err ) {
516517 return callback ( err ) ;
517518 }
@@ -521,12 +522,12 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
521522
522523 // if we have a known cluster time, gossip it
523524 if ( clusterTime ) {
524- finalCmd . $clusterTime = clusterTime ;
525+ cmd . $clusterTime = clusterTime ;
525526 }
526527
527528 if ( isSharded ( this ) && ! shouldUseOpMsg && readPreference && readPreference . mode !== 'primary' ) {
528- finalCmd = {
529- $query : finalCmd ,
529+ cmd = {
530+ $query : cmd ,
530531 $readPreference : readPreference . toJSON ( )
531532 } ;
532533 }
@@ -544,8 +545,8 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
544545
545546 const cmdNs = `${ ns . db } .$cmd` ;
546547 const message = shouldUseOpMsg
547- ? new Msg ( cmdNs , finalCmd , commandOptions )
548- : new Query ( cmdNs , finalCmd , commandOptions ) ;
548+ ? new Msg ( cmdNs , cmd , commandOptions )
549+ : new Query ( cmdNs , cmd , commandOptions ) ;
549550
550551 try {
551552 write ( this , message , commandOptions , callback ) ;
0 commit comments