Skip to main content
Glama

EstimateDocumentCount

Quickly estimate the number of documents in a collection in Astra DB using a fast, approximate counting method to support efficient data management.

Instructions

Estimate the number of documents in a collection using a fast, approximate count method

Input Schema

NameRequiredDescriptionDefault
collectionNameYesName of the collection to estimate document count for

Input Schema (JSON Schema)

{ "properties": { "collectionName": { "description": "Name of the collection to estimate document count for", "type": "string" } }, "required": [ "collectionName" ], "type": "object" }

Implementation Reference

  • The main handler function implementing the EstimateDocumentCount tool. It estimates the document count in the given collection using estimatedDocumentCount() if available, falling back to fetching all documents and counting them.
    export async function EstimateDocumentCount(params: { collectionName: string; }) { const { collectionName } = params; const collection = db.collection(collectionName); try { // Try to use estimatedDocumentCount if available if (typeof collection.estimatedDocumentCount === 'function') { return await collection.estimatedDocumentCount(); } else { // Last resort: get all documents and count them const cursor = collection.find({}); // Handle the case when toArray is not available if (!cursor || typeof cursor.toArray !== 'function') { console.warn(`cursor.toArray is not available for collection '${collectionName}'`); return 0; } const documents = await cursor.toArray(); return documents.length; } } catch (error) { console.error(`Error estimating document count for collection '${collectionName}':`, error); return 0; } }
  • The tool registration entry defining the name, description, and input schema (requiring collectionName string). This is part of the exported tools array used for listing tools.
    { name: "EstimateDocumentCount", description: "Estimate the number of documents in a collection using a fast, approximate count method", inputSchema: { type: "object", properties: { collectionName: { type: "string", description: "Name of the collection to estimate document count for", }, }, required: ["collectionName"], }, },
  • index.ts:100-111 (registration)
    The switch case in the server tool call handler that dispatches to the EstimateDocumentCount function and formats the response.
    case "EstimateDocumentCount": const count = await EstimateDocumentCount({ collectionName: args.collectionName as string, }); return { content: [ { type: "text", text: `Estimated document count in "${args.collectionName}": ${count}`, }, ], };
  • index.ts:42-42 (registration)
    Import statement for the EstimateDocumentCount handler function.
    import { EstimateDocumentCount } from "./tools/EstimateDocumentCount.js";

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/datastax/astra-db-mcp'

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