Skip to main content
Glama

MongoDB MCP Server

Official
by mongodb-js

collection-storage-size

This tool retrieves the storage size of a specified MongoDB collection, enabling users to monitor and manage database resources effectively. It requires database and collection names as inputs to deliver precise results.

Instructions

Gets the size of the collection

Input Schema

NameRequiredDescriptionDefault
collectionYesCollection name
databaseYesDatabase name

Input Schema (JSON Schema)

{ "$schema": "http://json-schema.org/draft-07/schema#", "additionalProperties": false, "properties": { "collection": { "description": "Collection name", "type": "string" }, "database": { "description": "Database name", "type": "string" } }, "required": [ "database", "collection" ], "type": "object" }

Implementation Reference

  • Executes an aggregation pipeline on the specified collection using $collStats to retrieve storage statistics, sums the size, scales it using getStats helper, and returns a formatted text response with the size in appropriate units.
    protected async execute({ database, collection }: ToolArgs<typeof DbOperationArgs>): Promise<CallToolResult> { const provider = await this.ensureConnected(); const [{ value }] = (await provider .aggregate(database, collection, [ { $collStats: { storageStats: {} } }, { $group: { _id: null, value: { $sum: "$storageStats.size" } } }, ]) .toArray()) as [{ value: number }]; const { units, value: scaledValue } = CollectionStorageSizeTool.getStats(value); return { content: [ { text: `The size of "${database}.${collection}" is \`${scaledValue.toFixed(2)} ${units}\``, type: "text", }, ], }; }
  • Zod schema defining the required input arguments (database and collection names) for the collection-storage-size tool and other MongoDB database operation tools.
    export const DbOperationArgs = { database: z.string().describe("Database name"), collection: z.string().describe("Collection name"), };
  • Static helper method that converts raw byte size to human-readable format with appropriate units (bytes, KB, MB, GB).
    private static getStats(value: number): { value: number; units: string } { const kb = 1024; const mb = kb * 1024; const gb = mb * 1024; if (value > gb) { return { value: value / gb, units: "GB" }; } if (value > mb) { return { value: value / mb, units: "MB" }; } if (value > kb) { return { value: value / kb, units: "KB" }; } return { value, units: "bytes" }; } }
  • Aggregates all tool classes, including those from MongoDB tools.ts (which exports CollectionStorageSizeTool), into a single AllTools array for registration with the MCP server.
    export const AllTools: ToolClass[] = Object.values({ ...MongoDbTools, ...AtlasTools, ...AtlasLocalTools, });
  • Re-exports the CollectionStorageSizeTool class to make it available for inclusion in the AllTools registry.
    export { CollectionStorageSizeTool } from "./metadata/collectionStorageSize.js";

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mongodb-js/mongodb-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server