22* Sharding Countly collections when DB requires authentication, provide it to authDB.auth command in the code
33* Server: mongodb
44* Path: any
5- * Command: mongosh < sharding.js
5+ * Command: mongosh -u uname -p 'password' --authenticationDatabase admin sharding.js
66*/
77
8- /* global Mongo, print, printjson */
9- var COUNTLY_DRILL = 'countly_drill' ,
10- COUNTLY = 'countly' ,
11- COUNT_TO_SHARD = 100000 ;
8+ /* global db, print, printjson */
9+
10+ // Set countly_drill database name
11+ const COUNTLY_DRILL = 'countly_drill' ;
12+ // Set countly database name
13+ const COUNTLY = 'countly' ;
14+ // Set the threshold for sharding collections
15+ const COUNT_TO_SHARD = 100000 ;
1216
1317var EXCEPTIONS = [
1418 / ^ s y s t e m \. / ,
@@ -27,14 +31,8 @@ var COUNTLY_TO_SHARD = [
2731 "feedback" ,
2832] ;
2933
30- var conn = new Mongo ( ) ,
31- authDB = conn . getDB ( 'admin' ) ;
32-
33- // need to update this info
34- authDB . auth ( '<username>' , '<password>' ) ;
35-
36- var cly = conn . getDB ( COUNTLY ) ,
37- drill = conn . getDB ( COUNTLY_DRILL ) ;
34+ var cly = db . getSiblingDB ( COUNTLY ) ,
35+ drill = db . getSiblingDB ( COUNTLY_DRILL ) ;
3836
3937var clyCollections = cly . getCollectionNames ( ) , collections = clyCollections . concat ( drill . getCollectionNames ( ) ) , check = [ ] ;
4038
@@ -61,8 +59,7 @@ check.forEach(function(c) {
6159 var db = clyCollections . indexOf ( c ) === - 1 ? drill : cly ,
6260 dbName = clyCollections . indexOf ( c ) === - 1 ? COUNTLY_DRILL : COUNTLY ,
6361 count = db [ c ] . count ( ) ,
64- capped = db [ c ] . stats ( ) [ 'capped' ] ,
65- status = db [ c ] . getShardVersion ( ) . ok ;
62+ capped = db [ c ] . stats ( ) [ 'capped' ] ;
6663
6764 COUNTLY_TO_SHARD . some ( ( e ) => {
6865 if ( c . indexOf ( e ) == 0 ) {
@@ -71,7 +68,7 @@ check.forEach(function(c) {
7168 }
7269 } ) ;
7370
74- if ( ! capped && count > COUNT_TO_SHARD && ! status && ! exceptional ) {
71+ if ( ! capped && count > COUNT_TO_SHARD && ! exceptional ) {
7572 print ( 'Creating hashed index & enabling sharding for collection "' + c + '"... ' ) ;
7673
7774 db . getCollection ( c ) . createIndex ( { _id : 'hashed' } ) ;
0 commit comments