@@ -53,6 +53,7 @@ import type {
5353 SearchIndexDescription ,
5454 ReadPreferenceMode ,
5555} from 'mongodb' ;
56+ import { ReadPreference } from 'mongodb' ;
5657import ConnectionStringUrl from 'mongodb-connection-string-url' ;
5758import parseNamespace from 'mongodb-ns' ;
5859import type {
@@ -133,17 +134,21 @@ function isReadPreferenceSet(connectionString: string): boolean {
133134 ) ;
134135}
135136
136- function isReadPreferenceTagsSet ( connectionString : string ) : boolean {
137- return ! ! new ConnectionStringUrl ( connectionString ) . searchParams . get (
138- 'readPreferenceTags'
139- ) ;
137+ function readPreferenceWithoutTags ( readPreference : ReadPreference ) {
138+ return new ReadPreference ( readPreference . mode , undefined , {
139+ maxStalenessSeconds : readPreference . maxStalenessSeconds ,
140+ hedge : readPreference . hedge ,
141+ } ) ;
140142}
141143
142- function maybeOverrideReadPreference ( connectionString : string ) : {
143- readPreference ?: 'secondaryPreferred' ;
144+ function maybeOverrideReadPreference (
145+ isMongos : boolean ,
146+ readPreference ?: ReadPreference
147+ ) : {
148+ readPreference ?: ReadPreference ;
144149} {
145- if ( isReadPreferenceTagsSet ( connectionString ) ) {
146- return { readPreference : 'secondaryPreferred' } ;
150+ if ( isMongos && readPreference ?. tags ) {
151+ return { readPreference : readPreferenceWithoutTags ( readPreference ) } ;
147152 }
148153
149154 return { } ;
@@ -1283,7 +1288,8 @@ class DataServiceImpl extends WithLogContext implements DataService {
12831288 {
12841289 nameOnly,
12851290 ...maybeOverrideReadPreference (
1286- this . _connectionOptions . connectionString
1291+ this . isMongos ( ) ,
1292+ this . _crudClient ?. readPreference
12871293 ) ,
12881294 }
12891295 ) ;
@@ -1423,7 +1429,8 @@ class DataServiceImpl extends WithLogContext implements DataService {
14231429 {
14241430 enableUtf8Validation : false ,
14251431 ...maybeOverrideReadPreference (
1426- this . _connectionOptions . connectionString
1432+ this . isMongos ( ) ,
1433+ this . _crudClient ?. readPreference
14271434 ) ,
14281435 }
14291436 ) ;
@@ -2571,7 +2578,8 @@ class DataServiceImpl extends WithLogContext implements DataService {
25712578 {
25722579 enableUtf8Validation : false ,
25732580 ...maybeOverrideReadPreference (
2574- this . _connectionOptions . connectionString
2581+ this . isMongos ( ) ,
2582+ this . _crudClient ?. readPreference
25752583 ) ,
25762584 }
25772585 ) ;
0 commit comments