create-index
Generate indexes for MongoDB collections to optimize query performance. Specify database, collection, and index keys to enhance data retrieval efficiency.
Instructions
Create an index for a collection
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | Collection name | |
| database | Yes | Database name | |
| keys | Yes | The index definition | |
| name | No | The name of the index |
Implementation Reference
- The handler function that executes the index creation logic for classic, vectorSearch, and search index types using the MongoDB provider.protected async execute({ database, collection, name, definition: definitions, }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> { const provider = await this.ensureConnected(); let indexes: string[] = []; const definition = definitions[0]; if (!definition) { throw new Error("Index definition not provided. Expected one of the following: `classic`, `vectorSearch`"); } let responseClarification = ""; switch (definition.type) { case "classic": indexes = await provider.createIndexes(database, collection, [ { key: definition.keys, name, }, ]); break; case "vectorSearch": { await this.ensureSearchIsSupported(); indexes = await provider.createSearchIndexes(database, collection, [ { name, definition: { fields: definition.fields, }, type: "vectorSearch", }, ]); responseClarification = " Since this is a vector search index, it may take a while for the index to build. Use the `list-indexes` tool to check the index status."; // clean up the embeddings cache so it considers the new index this.session.vectorSearchEmbeddingsManager.cleanupEmbeddingsForNamespace({ database, collection }); } break; case "search": { await this.ensureSearchIsSupported(); indexes = await provider.createSearchIndexes(database, collection, [ { name, definition: { mappings: definition.mappings, analyzer: definition.analyzer, numPartitions: definition.numPartitions, }, type: "search", }, ]); responseClarification = " Since this is a search index, it may take a while for the index to build. Use the `list-indexes` tool to check the index status."; } break; } return { content: [ { text: `Created the index "${indexes[0]}" on collection "${collection}" in database "${database}".${responseClarification}`, type: "text", }, ], }; }
- Defines the Zod schema for tool inputs: database, collection, optional index name, and discriminated union for index definitions (classic, vectorSearch, atlas search).protected argsShape = { ...DbOperationArgs, name: z.string().optional().describe("The name of the index"), definition: z .array( z.discriminatedUnion("type", [ z .object({ type: z.literal("classic"), keys: z.object({}).catchall(z.custom<IndexDirection>()).describe("The index definition"), }) .describe("Definition for a MongoDB index (e.g. ascending/descending/geospatial)."), ...(this.isFeatureEnabled("search") ? [this.vectorSearchIndexDefinition, this.atlasSearchIndexDefinition] : []), ]) ) .describe( `The index definition. Use 'classic' for standard indexes${this.isFeatureEnabled("search") ? ", 'vectorSearch' for vector search indexes, and 'search' for Atlas Search (lexical) indexes" : ""}.` ), };
- src/tools/mongodb/create/createIndex.ts:134-135 (registration)Sets the tool name to 'create-index' and description.public name = "create-index"; protected description = "Create an index for a collection";
- src/tools/mongodb/tools.ts:5-5 (registration)Re-exports the CreateIndexTool for use in MongoDB tools module.export { CreateIndexTool } from "./create/createIndex.js";