create-index
Create indexes for MongoDB collections to improve query performance and enable efficient data retrieval.
Instructions
Create an index for a collection
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| database | Yes | Database name | |
| collection | Yes | Collection name | |
| name | No | The name of the index | |
| definition | Yes | The index definition. Use 'classic' for standard indexes. |
Implementation Reference
- The main handler function that connects to MongoDB and calls createIndexes to create the specified index on the collection.
protected async execute({ database, collection, keys, name, }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> { const provider = await this.ensureConnected(); const indexes = await provider.createIndexes(database, collection, [ { key: keys, name, }, ]); return { content: [ { text: `Created the index "${indexes[0]}" on collection "${collection}" in database "${database}"`, type: "text", }, ], }; } - Zod schema defining the input arguments for the create-index tool, including database, collection (from DbOperationArgs), keys for the index, and optional name.
protected argsShape = { ...DbOperationArgs, keys: z.record(z.string(), z.custom<IndexDirection>()).describe("The index definition"), name: z.string().optional().describe("The name of the index"), }; - src/tools/mongodb/tools.ts:22-43 (registration)Registers CreateIndexTool (imported from ./create/createIndex.js) in the array of all MongoDB tools.
export const MongoDbTools = [ ConnectTool, ListCollectionsTool, ListDatabasesTool, CollectionIndexesTool, CreateIndexTool, CollectionSchemaTool, FindTool, InsertManyTool, DeleteManyTool, CollectionStorageSizeTool, CountTool, DbStatsTool, AggregateTool, UpdateManyTool, RenameCollectionTool, DropDatabaseTool, DropCollectionTool, ExplainTool, CreateCollectionTool, LogsTool, ]; - src/tools/mongodb/create/createIndex.ts:8-8 (registration)Defines the tool name as 'create-index' within the CreateIndexTool class.
protected name = "create-index";