Skip to main content
Glama
jonfreeland

MongoDB MCP Server

by jonfreeland

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

TableJSON Schema
NameRequiredDescriptionDefault
collectionYesCollection name
databaseNoDatabase name (optional if default database is configured)
filterNoMongoDB query filter (optional, defaults to count all documents)

Implementation Reference

  • 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), }, ], }; }
  • 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'], }, },

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jonfreeland/mongodb-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server