@@ -92,18 +92,23 @@ class MongoClientInterface {
92
92
constructor ( params ) {
93
93
const { replicaSetHosts, writeConcern, replicaSet, readPreference, path,
94
94
database, logger, replicationGroupId, authCredentials,
95
- isLocationTransient } = params ;
95
+ isLocationTransient, shardCollections } = params ;
96
96
const cred = MongoUtils . credPrefix ( authCredentials ) ;
97
97
this . mongoUrl = `mongodb://${ cred } ${ replicaSetHosts } /` +
98
- `?w=${ writeConcern } &replicaSet=${ replicaSet } ` +
99
- `&readPreference=${ readPreference } ` ;
98
+ `?w=${ writeConcern } &readPreference=${ readPreference } ` ;
99
+
100
+ if ( ! shardCollections ) {
101
+ this . mongoUrl += `&replicaSet=${ replicaSet } ` ;
102
+ }
103
+
100
104
this . logger = logger ;
101
105
this . client = null ;
102
106
this . db = null ;
103
107
this . path = path ;
104
108
this . replicationGroupId = replicationGroupId ;
105
109
this . database = database ;
106
110
this . isLocationTransient = isLocationTransient ;
111
+ this . shardCollections = shardCollections ;
107
112
108
113
this . concurrentCursors = ( process . env . CONCURRENT_CURSORS &&
109
114
! Number . isNaN ( process . env . CONCURRENT_CURSORS ) )
@@ -142,6 +147,7 @@ class MongoClientInterface {
142
147
this . db = client . db ( this . database , {
143
148
ignoreUndefined : true ,
144
149
} ) ;
150
+ this . adminDb = client . db ( 'admin' ) ;
145
151
return this . usersBucketHack ( cb ) ;
146
152
} ) ;
147
153
}
@@ -216,9 +222,24 @@ class MongoClientInterface {
216
222
if ( err ) {
217
223
log . error (
218
224
'createBucket: error creating bucket' ,
219
- { error : err . message } ) ;
225
+ { error : err } ) ;
220
226
return cb ( errors . InternalError ) ;
221
227
}
228
+ if ( this . shardCollections ) {
229
+ const cmd = {
230
+ shardCollection : `${ this . database } .${ bucketName } ` ,
231
+ key : { _id : 1 } ,
232
+ } ;
233
+ return this . adminDb . command ( cmd , { } , err => {
234
+ if ( err ) {
235
+ log . error (
236
+ 'createBucket: enabling sharding' ,
237
+ { error : err } ) ;
238
+ return cb ( errors . InternalError ) ;
239
+ }
240
+ return cb ( ) ;
241
+ } ) ;
242
+ }
222
243
return cb ( ) ;
223
244
} ) ;
224
245
}
@@ -1706,4 +1727,3 @@ class MongoClientInterface {
1706
1727
}
1707
1728
1708
1729
module . exports = MongoClientInterface ;
1709
-
0 commit comments