getCollection
Retrieve documents from a MongoDB collection using query filters, with options to limit results and specify field projections.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collectionName | Yes | ||
| limit | No | ||
| query | No | ||
| projection | No |
Implementation Reference
- src/index.ts:56-94 (handler)The handler function for the 'getCollection' tool. It connects to the MongoDB database if necessary, retrieves the specified collection, queries documents using optional parameters (limit, query, projection), and returns the results as a formatted text response.async ({ collectionName, limit, query, projection, }: { collectionName: string; limit?: number; query?: any; projection?: any; }) => { try { let db = mongodbConnection.getDb(); if (!db) { await mongodbConnection.connect(this.MONGODB_URI); db = mongodbConnection.getDb(); if (!db) throw new Error("Failed to connect to database"); } const collection = db.collection(collectionName); const documents = await collection .find(query ?? {}) .limit(limit ?? 100) .project(projection ?? {}) .toArray(); return { content: [ { type: "text", text: documents.map((d) => JSON.stringify(d)).join("\n"), }, ], }; } catch (error) { console.error(error); return { content: [{ type: "text", text: "Error: " + error }], }; } }
- src/index.ts:50-55 (schema)The Zod schema defining input parameters for the 'getCollection' tool: collectionName (required string), optional limit (number 1-1000, default 10), optional query and projection objects.{ collectionName: z.string(), limit: z.number().min(1).max(1000).optional().default(10), query: z.object({}).optional(), projection: z.object({}).optional(), },
- src/index.ts:48-95 (registration)The registration of the 'getCollection' tool using this.mcpServer.tool(), including name, input schema, and handler function.this.mcpServer.tool( "getCollection", { collectionName: z.string(), limit: z.number().min(1).max(1000).optional().default(10), query: z.object({}).optional(), projection: z.object({}).optional(), }, async ({ collectionName, limit, query, projection, }: { collectionName: string; limit?: number; query?: any; projection?: any; }) => { try { let db = mongodbConnection.getDb(); if (!db) { await mongodbConnection.connect(this.MONGODB_URI); db = mongodbConnection.getDb(); if (!db) throw new Error("Failed to connect to database"); } const collection = db.collection(collectionName); const documents = await collection .find(query ?? {}) .limit(limit ?? 100) .project(projection ?? {}) .toArray(); return { content: [ { type: "text", text: documents.map((d) => JSON.stringify(d)).join("\n"), }, ], }; } catch (error) { console.error(error); return { content: [{ type: "text", text: "Error: " + error }], }; } } );
- src/mongodb/index.ts:1-33 (helper)The MongoDBConnection class and singleton instance used by the getCollection handler for database connections.import { MongoClient, Db } from "mongodb"; export class MongoDBConnection { private client: MongoClient | null = null; private db: Db | null = null; async connect(databaseUrl: string) { try { this.client = new MongoClient(databaseUrl); await this.client.connect(); this.db = this.client.db(); return this.db; } catch (error) { console.error("MongoDB connection error:", error); throw error; } } async close() { await this.client?.close(); } getClient(): MongoClient | null { return this.client; } getDb(): Db | null { return this.db; } } export const mongodbConnection = new MongoDBConnection();