@@ -27,7 +27,7 @@ import type {
27
27
bson ,
28
28
} from '@mongosh/service-provider-core' ;
29
29
import type { ClientSideFieldLevelEncryptionOptions } from './field-level-encryption' ;
30
- import type { AutoEncryptionOptions } from 'mongodb' ;
30
+ import { type AutoEncryptionOptions } from 'mongodb' ;
31
31
import { shellApiType } from './enums' ;
32
32
import type { AbstractCursor } from './abstract-cursor' ;
33
33
import type ChangeStreamCursor from './change-stream-cursor' ;
@@ -92,6 +92,17 @@ function getAssertCaller(caller?: string): string {
92
92
return caller ? ` (${ caller } )` : '' ;
93
93
}
94
94
95
+ // Fields to add to a $match/filter on config.collections to only get
96
+ // collections that are actually sharded
97
+ export const onlyShardedCollectionsInConfigFilter = {
98
+ // dropped is gone on newer server versions, so check for !== true
99
+ // rather than for === false (SERVER-51880 and related)
100
+ dropped : { $ne : true } ,
101
+ // unsplittable introduced in PM-3364 to mark unsharded collections
102
+ // that are still being tracked in the catalog
103
+ unsplittable : { $ne : true } ,
104
+ } as const ;
105
+
95
106
export function assertArgsDefinedType (
96
107
args : any [ ] ,
97
108
expectedTypes : Array < true | string | Array < string | undefined > > ,
@@ -487,33 +498,45 @@ export async function getPrintableShardStatus(
487
498
] ) ;
488
499
result . balancer = balancerRes ;
489
500
490
- const databases = await ( await configDB . getCollection ( 'databases' ) . find ( ) )
491
- . sort ( { name : 1 } )
492
- . toArray ( ) ;
493
-
501
+ // All databases in config.databases + those implicitly referenced
502
+ // by a sharded collection in config.collections
503
+ // (could become a single pipeline using $unionWith when we drop 4.2 server support)
504
+ const [ databases , collections ] = await Promise . all ( [
505
+ ( async ( ) =>
506
+ await ( await configDB . getCollection ( 'databases' ) . find ( ) )
507
+ . sort ( { _id : 1 } )
508
+ . toArray ( ) ) ( ) ,
509
+ ( async ( ) =>
510
+ await (
511
+ await configDB . getCollection ( 'collections' ) . find ( {
512
+ ...onlyShardedCollectionsInConfigFilter ,
513
+ } )
514
+ )
515
+ . sort ( { _id : 1 } )
516
+ . toArray ( ) ) ( ) ,
517
+ ] ) ;
494
518
// Special case the config db, since it doesn't have a record in config.databases.
495
519
databases . push ( { _id : 'config' , primary : 'config' , partitioned : true } ) ;
520
+
521
+ for ( const coll of collections ) {
522
+ if ( ! databases . find ( ( db ) => coll . _id . startsWith ( db . _id + '.' ) ) ) {
523
+ databases . push ( { _id : coll . _id . split ( '.' ) [ 0 ] } ) ;
524
+ }
525
+ }
526
+
496
527
databases . sort ( ( a : any , b : any ) : number => {
497
528
return a . _id . localeCompare ( b . _id ) ;
498
529
} ) ;
499
530
500
- result . databases = await Promise . all (
501
- databases . map ( async ( db ) => {
502
- const escapeRegex = ( string : string ) : string => {
503
- return string . replace ( / [ - / \\ ^ $ * + ? . ( ) | [ \] { } ] / g, '\\$&' ) ;
504
- } ;
505
- const colls = await (
506
- await configDB
507
- . getCollection ( 'collections' )
508
- . find ( { _id : new RegExp ( '^' + escapeRegex ( db . _id ) + '\\.' ) } )
509
- )
510
- . sort ( { _id : 1 } )
511
- . toArray ( ) ;
531
+ result . databases = (
532
+ await Promise . all (
533
+ databases . map ( async ( db ) => {
534
+ const colls = collections . filter ( ( coll ) =>
535
+ coll . _id . startsWith ( db . _id + '.' )
536
+ ) ;
512
537
513
- const collList : any = await Promise . all (
514
- colls
515
- . filter ( ( coll ) => ! coll . dropped )
516
- . map ( async ( coll ) => {
538
+ const collList = await Promise . all (
539
+ colls . map ( async ( coll ) => {
517
540
const collRes = { } as any ;
518
541
collRes . shardKey = coll . key ;
519
542
collRes . unique = ! ! coll . unique ;
@@ -605,12 +628,13 @@ export async function getPrintableShardStatus(
605
628
}
606
629
collRes . chunks = chunksRes ;
607
630
collRes . tags = tagsRes ;
608
- return [ coll . _id , collRes ] ;
631
+ return [ coll . _id , collRes ] as const ;
609
632
} )
610
- ) ;
611
- return { database : db , collections : Object . fromEntries ( collList ) } ;
612
- } )
613
- ) ;
633
+ ) ;
634
+ return { database : db , collections : Object . fromEntries ( collList ) } ;
635
+ } )
636
+ )
637
+ ) . filter ( ( dbEntry ) => ! ! dbEntry ) ;
614
638
615
639
delete result . shardingVersion . currentVersion ;
616
640
return result ;
0 commit comments