mongo-count-documents
Count documents in a MongoDB collection using database name, collection name, and optional query filters to get accurate document totals for data analysis and monitoring.
Instructions
Count documents in a MongoDB collection
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | Collection name | |
| database | Yes | Database name | |
| filter | No | Query filter as JSON object (optional) |
Input Schema (JSON Schema)
{
"properties": {
"collection": {
"description": "Collection name",
"type": "string"
},
"database": {
"description": "Database name",
"type": "string"
},
"filter": {
"additionalProperties": {},
"description": "Query filter as JSON object (optional)",
"type": "object"
}
},
"required": [
"database",
"collection"
],
"type": "object"
}
Implementation Reference
- src/index.ts:272-290 (handler)Handler function that connects to the MongoDB database, retrieves the specified collection, counts the documents matching the optional filter using countDocuments(), and returns the count in a formatted text response.async ({ database: dbName, collection: collectionName, filter = {} }) => { try { const db = await ensureConnection(dbName); const collection: Collection = db.collection(collectionName); const count = await collection.countDocuments(filter); return { content: [ { type: "text", text: `Found ${count} document(s) matching the filter`, }, ], }; } catch (error) { throw new Error(`Failed to count documents: ${error instanceof Error ? error.message : 'Unknown error'}`); } }
- src/index.ts:267-271 (schema)Zod input schema defining required 'database' and 'collection' strings, and optional 'filter' object for querying documents.{ database: z.string().describe("Database name"), collection: z.string().describe("Collection name"), filter: z.record(z.any()).optional().describe("Query filter as JSON object (optional)"), },
- src/index.ts:264-291 (registration)Registers the 'mongo-count-documents' tool on the MCP server with its description, input schema, and handler function.server.tool( "mongo-count-documents", "Count documents in a MongoDB collection", { database: z.string().describe("Database name"), collection: z.string().describe("Collection name"), filter: z.record(z.any()).optional().describe("Query filter as JSON object (optional)"), }, async ({ database: dbName, collection: collectionName, filter = {} }) => { try { const db = await ensureConnection(dbName); const collection: Collection = db.collection(collectionName); const count = await collection.countDocuments(filter); return { content: [ { type: "text", text: `Found ${count} document(s) matching the filter`, }, ], }; } catch (error) { throw new Error(`Failed to count documents: ${error instanceof Error ? error.message : 'Unknown error'}`); } } );
- src/index.ts:88-100 (helper)Helper function to establish MongoDB connection if needed and return the database instance, used by the tool handler.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)!; }