count_documents
Count documents in a MongoDB collection using a specified filter. Efficiently determine data volume, optimize query strategies, and implement pagination without retrieving full documents.
Instructions
Count documents in a collection that match a filter.
Benefits:
More efficient than retrieving full documents
Good for understanding data volume
Can help planning query strategies
Optimize pagination implementation
Example: use_mcp_tool with server_name: "mongodb", tool_name: "count_documents", arguments: { "collection": "users", "filter": { "active": true, "age": { "$gte": 21 } } }
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | Collection name | |
| database | No | Database name (optional if default database is configured) | |
| filter | No | MongoDB query filter (optional, defaults to count all documents) |
Implementation Reference
- src/index.ts:1359-1384 (handler)Handler function for the 'count_documents' tool. Connects to the database, executes countDocuments on the specified collection with optional filter, and returns the count as JSON.case 'count_documents': { const { database, collection, filter = {} } = request.params.arguments as { database?: string; collection: string; filter?: object; }; const dbName = database || this.defaultDatabase; if (!dbName) { throw new McpError( ErrorCode.InvalidRequest, 'Database name is required when no default database is configured' ); } const db = client.db(dbName); const count = await db.collection(collection).countDocuments(filter); return { content: [ { type: 'text', text: JSON.stringify({ count }, null, 2), }, ], }; }
- src/index.ts:774-791 (schema)Input schema definition for the 'count_documents' tool, specifying parameters like database, collection, and optional filter.inputSchema: { type: 'object', properties: { database: { type: 'string', description: 'Database name (optional if default database is configured)', }, collection: { type: 'string', description: 'Collection name', }, filter: { type: 'object', description: 'MongoDB query filter (optional, defaults to count all documents)', }, }, required: ['collection'], },
- src/index.ts:756-792 (registration)Registration of the 'count_documents' tool in the ListTools response, including name, description, and input schema.{ name: 'count_documents', description: `Count documents in a collection that match a filter. Benefits: - More efficient than retrieving full documents - Good for understanding data volume - Can help planning query strategies - Optimize pagination implementation Example: use_mcp_tool with server_name: "mongodb", tool_name: "count_documents", arguments: { "collection": "users", "filter": { "active": true, "age": { "$gte": 21 } } }`, inputSchema: { type: 'object', properties: { database: { type: 'string', description: 'Database name (optional if default database is configured)', }, collection: { type: 'string', description: 'Collection name', }, filter: { type: 'object', description: 'MongoDB query filter (optional, defaults to count all documents)', }, }, required: ['collection'], }, },