@@ -60,6 +60,20 @@ func GetHelloResponse(ctx context.Context, client *mongo.Client) (HelloResponse,
6060 return runCommand [HelloResponse ](ctx , client , "hello" )
6161}
6262
63+ type CollStats struct {
64+ // uncompressed
65+ Size int64 `bson:"size"`
66+ // compressed
67+ StorageSize int64 `bson:"storageSize"`
68+ }
69+
70+ func GetCollStats (ctx context.Context , client * mongo.Client , database string , collection string ) (CollStats , error ) {
71+ return runDatabaseCommand [CollStats ](ctx , client , database , bson.D {
72+ {Key : "collStats" , Value : collection },
73+ {Key : "scale" , Value : 1 },
74+ })
75+ }
76+
6377func runCommand [T any ](ctx context.Context , client * mongo.Client , command string ) (T , error ) {
6478 var result T
6579 singleResult := client .Database ("admin" ).RunCommand (ctx , bson.D {
@@ -74,3 +88,16 @@ func runCommand[T any](ctx context.Context, client *mongo.Client, command string
7488 }
7589 return result , nil
7690}
91+
92+ func runDatabaseCommand [T any ](ctx context.Context , client * mongo.Client , database string , commandDoc bson.D ) (T , error ) {
93+ var result T
94+ singleResult := client .Database (database ).RunCommand (ctx , commandDoc )
95+ if singleResult .Err () != nil {
96+ return result , fmt .Errorf ("command failed: %v" , singleResult .Err ())
97+ }
98+
99+ if err := singleResult .Decode (& result ); err != nil {
100+ return result , fmt .Errorf ("command decoding failed: %v" , err )
101+ }
102+ return result , nil
103+ }
0 commit comments