MongoDB MCP Server

count_documents

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

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

Input Schema (JSON Schema)

{ "properties": { "collection": { "description": "Collection name", "type": "string" }, "database": { "description": "Database name (optional if default database is configured)", "type": "string" }, "filter": { "description": "MongoDB query filter (optional, defaults to count all documents)", "type": "object" } }, "required": [ "collection" ], "type": "object" }