mongo-aggregate
Execute aggregation pipelines on MongoDB collections to process, transform, and analyze data through multi-stage operations.
Instructions
Execute aggregation pipeline on a MongoDB collection
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| database | Yes | Database name | |
| collection | Yes | Collection name | |
| pipeline | Yes | Aggregation pipeline as array of stage objects |
Implementation Reference
- src/index.ts:241-261 (handler)The asynchronous handler function that ensures MongoDB connection, executes the aggregation pipeline using collection.aggregate(), retrieves results as array, formats output with truncateForOutput, and returns formatted text content.async ({ database: dbName, collection: collectionName, pipeline }) => { try { const db = await ensureConnection(dbName); const collection: Collection = db.collection(collectionName); const documents = await collection.aggregate(pipeline).toArray(); const formattedOutput = formatJsonOutput(documents); return { content: [ { type: "text", text: `Aggregation returned ${documents.length} document(s):\n\n${formattedOutput}`, }, ], }; } catch (error) { throw new Error(`Failed to execute aggregation: ${error instanceof Error ? error.message : 'Unknown error'}`); } }
- src/index.ts:236-240 (schema)Zod input schema defining required parameters: database name, collection name, and pipeline as array of stage objects.{ database: z.string().describe("Database name"), collection: z.string().describe("Collection name"), pipeline: z.array(z.record(z.any())).describe("Aggregation pipeline as array of stage objects"), },
- src/index.ts:233-262 (registration)Tool registration via server.tool() with name 'mongo-aggregate', description, input schema, and handler reference.server.tool( "mongo-aggregate", "Execute aggregation pipeline on a MongoDB collection", { database: z.string().describe("Database name"), collection: z.string().describe("Collection name"), pipeline: z.array(z.record(z.any())).describe("Aggregation pipeline as array of stage objects"), }, async ({ database: dbName, collection: collectionName, pipeline }) => { try { const db = await ensureConnection(dbName); const collection: Collection = db.collection(collectionName); const documents = await collection.aggregate(pipeline).toArray(); const formattedOutput = formatJsonOutput(documents); return { content: [ { type: "text", text: `Aggregation returned ${documents.length} document(s):\n\n${formattedOutput}`, }, ], }; } catch (error) { throw new Error(`Failed to execute aggregation: ${error instanceof Error ? error.message : 'Unknown error'}`); } } );
- src/index.ts:88-100 (helper)Helper function to ensure MongoDB client connection and database instance, cached per database.async function ensureConnection(dbName: string): Promise<Db> { if (!mongoClient) { const uri = getMongoUri(); mongoClient = new MongoClient(uri); await mongoClient.connect(); } if (!databases.has(dbName)) { databases.set(dbName, mongoClient.db(dbName)); } return databases.get(dbName)!; }
- src/index.ts:64-78 (helper)Helper function to format and truncate JSON output for large results, used in tool responses.function formatJsonOutput(data: unknown): string { const truncatedData = truncateForOutput(data); let outputText = JSON.stringify(truncatedData, null, 2); outputText = outputText.replace( /"\.\.\.(\d+) more items"/g, "...$1 more items" ); outputText = outputText.replace( /"\.\.\.(\d+) more properties": "\.\.\.?"/g, "...$1 more properties" ); return outputText; }