@@ -95,7 +95,8 @@ class MongoClientInterface {
95
95
isLocationTransient } = params ;
96
96
const cred = MongoUtils . credPrefix ( authCredentials ) ;
97
97
this . mongoUrl = `mongodb://${ cred } ${ replicaSetHosts } /` +
98
- `?w=${ writeConcern } &replicaSet=${ replicaSet } ` +
98
+ `?w=${ writeConcern } ` +
99
+ `${ ! ! replicaSet ? `&replicaSet=${ replicaSet } ` : '' } ` +
99
100
`&readPreference=${ readPreference } ` ;
100
101
this . logger = logger ;
101
102
this . client = null ;
@@ -104,6 +105,7 @@ class MongoClientInterface {
104
105
this . replicationGroupId = replicationGroupId ;
105
106
this . database = database ;
106
107
this . isLocationTransient = isLocationTransient ;
108
+ this . replicaSet = replicaSet ;
107
109
108
110
this . concurrentCursors = ( process . env . CONCURRENT_CURSORS &&
109
111
! Number . isNaN ( process . env . CONCURRENT_CURSORS ) )
@@ -142,6 +144,7 @@ class MongoClientInterface {
142
144
this . db = client . db ( this . database , {
143
145
ignoreUndefined : true ,
144
146
} ) ;
147
+ this . adminDb = client . db ( 'admin' ) ;
145
148
return this . usersBucketHack ( cb ) ;
146
149
} ) ;
147
150
}
@@ -219,6 +222,21 @@ class MongoClientInterface {
219
222
{ error : err . message } ) ;
220
223
return cb ( errors . InternalError ) ;
221
224
}
225
+ if ( this . replicaSet === undefined ) {
226
+ const cmd = {
227
+ shardCollection : `${ this . database } .${ bucketName } ` ,
228
+ key : { _id : 1 } ,
229
+ } ;
230
+ return this . adminDb . command ( cmd , { } , err => {
231
+ if ( err ) {
232
+ log . error (
233
+ 'createBucket: enabling sharding' ,
234
+ { error : err . message } ) ;
235
+ return cb ( errors . InternalError ) ;
236
+ }
237
+ return cb ( ) ;
238
+ } ) ;
239
+ }
222
240
return cb ( ) ;
223
241
} ) ;
224
242
}
0 commit comments