Estimate document count
Documents represent a single row or record of data in Astra DB Serverless databases.
You use the Collection
class to work with documents through the Data API clients.
For instructions to get a Collection
object, see Work with collections.
For general information about working with documents, including common operations and operators, see the Work with documents.
For more information about the Data API and clients, see Get started with the Data API.
Estimate document count in a collection
Get an approximate document count for an entire collection. Filtering isn’t supported. For the clients, you can set standard options, such as a timeout in milliseconds. There are no other options available.
In the estimatedDocumentCount
command’s response, the document count is based on current system statistics at the time the request is received by the database server.
Due to potential in-progress updates (document additions and deletions), the actual number of documents in the collection can be lower or higher in the database.
-
Python
-
TypeScript
-
Java
-
curl
For more information, see the Client reference.
Get an approximate document count for a collection:
collection.estimated_document_count()
Returns:
int
- A server-side estimate of the total number of documents in the collection.
Example response
37500
Example:
from astrapy import DataAPIClient
client = DataAPIClient("TOKEN")
database = client.get_database("API_ENDPOINT")
collection = database.collection
collection.estimated_document_count()
For more information, see the Client reference.
Get an approximate document count for a collection:
const estNumDocs = await collection.estimatedDocumentCount();
Returns:
Promise<number>
- A promise that resolves to a server-side estimate of the total number of documents in the collection.
Example:
import { DataAPIClient } from '@datastax/astra-db-ts';
// Reference an untyped collection
const client = new DataAPIClient('TOKEN');
const db = client.db('ENDPOINT', { keyspace: 'KEYSPACE' });
const collection = db.collection('COLLECTION');
(async function () {
console.log(await collection.estimatedDocumentCount());
})();
For more information, see the Client reference.
Get an approximate document count for a collection:
long estimatedDocumentCount();
long estimatedDocumentCount(EstimatedCountDocumentsOptions options);
Returns:
long
- A server-side estimate of the total number of documents in the collection. This estimate is built from the SSTable files.
Example:
package com.datastax.astra.client.collection;
import com.datastax.astra.client.Collection;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.exception.TooManyDocumentsToCountException;
import com.datastax.astra.client.model.Document;
import com.datastax.astra.client.model.EstimatedCountDocumentsOptions;
import com.datastax.astra.client.model.Filter;
import com.datastax.astra.client.model.Filters;
import com.datastax.astra.internal.command.LoggingCommandObserver;
import static com.datastax.astra.client.model.Filters.lt;
public class EstimateCountDocuments {
public static void main(String[] args) {
Collection<Document> collection = new DataAPIClient("TOKEN")
.getDatabase("API_ENDPOINT")
.getCollection("COLLECTION_NAME");
// Count with no filter
long estimatedCount = collection.estimatedDocumentCount();
// Count with options (adding a logger)
EstimatedCountDocumentsOptions options = new EstimatedCountDocumentsOptions()
.registerObserver("logger", new LoggingCommandObserver(DataAPIClient.class));
long estimateCount2 = collection.estimatedDocumentCount(options);
}
}
Use the estimatedDocumentCount
command to get an approximate document count for a collection:
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_COLLECTION" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{ "estimatedDocumentCount": {} }' | jq
Returns:
A successful request returns count
, which is an estimate of the total number of documents in the collection:
{ "status": { "count": 37500 } }