collection-indexes
Describe indexes for a MongoDB collection to understand query performance and optimize database operations by analyzing index structure and usage.
Instructions
Describe the indexes for a collection
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| database | Yes | Database name | |
| collection | Yes | Collection name |
Implementation Reference
- The main handler function that connects to MongoDB, fetches regular indexes and optional search indexes, formats them as JSON strings in the tool response.protected async execute({ database, collection }: ToolArgs<typeof DbOperationArgs>): Promise<CallToolResult> { const provider = await this.ensureConnected(); const indexes = await provider.getIndexes(database, collection); const indexDefinitions: IndexStatus[] = indexes.map((index) => ({ name: index.name as string, key: index.key as Document, })); const searchIndexDefinitions: SearchIndexStatus[] = []; if (this.isFeatureEnabled("search") && (await this.session.isSearchSupported())) { const searchIndexes = await provider.getSearchIndexes(database, collection); searchIndexDefinitions.push(...this.extractSearchIndexDetails(searchIndexes)); } return { content: [ ...formatUntrustedData( `Found ${indexDefinitions.length} indexes in the collection "${collection}":`, ...indexDefinitions.map((i) => JSON.stringify(i)) ), ...(searchIndexDefinitions.length > 0 ? formatUntrustedData( `Found ${searchIndexDefinitions.length} search and vector search indexes in the collection "${collection}":`, ...searchIndexDefinitions.map((i) => JSON.stringify(i)) ) : []), ], };
- Zod schema defining the input arguments 'database' and 'collection' used by the collection-indexes tool and other MongoDB tools.export const DbOperationArgs = { database: z.string().describe("Database name"), collection: z.string().describe("Collection name"), };
- src/tools/mongodb/metadata/collectionIndexes.ts:19-23 (registration)Tool class definition including the unique name 'collection-indexes', description, input schema reference, and operation type.export class CollectionIndexesTool extends MongoDBToolBase { public name = "collection-indexes"; protected description = "Describe the indexes for a collection"; protected argsShape = DbOperationArgs; static operationType: OperationType = "metadata";
- src/tools/mongodb/tools.ts:3-3 (registration)Re-export of the CollectionIndexesTool class from the MongoDB tools barrel file for easy import and registration.export { CollectionIndexesTool } from "./metadata/collectionIndexes.js";
- Helper method to extract relevant details from search index definitions, simplifying status, queryability, and definition for the response.protected extractSearchIndexDetails(indexes: Record<string, unknown>[]): SearchIndexStatus[] { return indexes.map((index) => ({ name: (index["name"] ?? "default") as string, type: (index["type"] ?? "UNKNOWN") as string, status: (index["status"] ?? "UNKNOWN") as string, queryable: (index["queryable"] ?? false) as boolean, latestDefinition: index["latestDefinition"] as Document, })); }