Count documents (Java)

Counts documents in a collection using filter clauses.

Exact counting of the number of documents is a slow, expensive operation. If the document count exceeds the upper limit set by you or by the API, an indication of this limit will be returned instead of the full count.

If you need to count large numbers of documents, consider using the method to estimate count or the DataStax Bulk Loader instead.

Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart.

Result

Returns an integer indicating the exact number of documents that match the specified filter.

If the count exceeds the caller-provided or API-set upper bound, the client raises TooManyDocumentsToCountException.

Parameters

Use the countDocuments method, which belongs to the com.datastax.astra.client.Collection class.

Method signature
int countDocuments(
  int upperBound
)
int countDocuments(
  Filter filter,
  int upperBound
)
int countDocuments(
  Filter filter,
  int upperBound,
  CountDocumentsOptions options
)
Name Type Summary

filter

Filter

Optional. An object that defines filter criteria using the Data API filter syntax. The method only finds documents that match the filter criteria. Filters can improve performance by reducing the number of documents that the Data API processes.

You must use & to escape any . or & in field names in the filter clause. You cannot use & to escape any other characters. For more information, see Work with . and & in field names (Java).

For a list of available filter operators and more examples, see Filter operators for collections (Java).

Filters can use only indexed fields. If you apply selective indexing when you create a collection, you cannot reference non-indexed fields in a filter.

upperBound

int

The maximum number of documents to count.

If the actual number of documents exceeds this value or exceeds the limit set by the Data API, the client will raise an error.

options

CountDocumentsOptions

Optional. The options for this operation. This class does not include any methods that are specific to the countDocuments method.

Examples

The following examples demonstrate how to count documents in a collection.

Count documents that match a filter

You can use a filter to count documents that match specific criteria.

For a list of available filter operators and more examples, see Filter operators for collections (Java).

Filters can use only indexed fields. If you apply selective indexing when you create a collection, you cannot reference non-indexed fields in a filter.

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.collections.exceptions.TooManyDocumentsToCountException;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;

public class Example {
  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    // Count documents
    Filter filter =
        Filters.and(Filters.eq("is_checked_out", false), Filters.lt("number_of_pages", 300));

    try {
      Integer result = collection.countDocuments(filter, 500);
      System.out.println(result);
    } catch (TooManyDocumentsToCountException error) {
      System.out.println("Number of documents exceeds upper bound or API limit");
    }
  }
}

Count all documents

To attempt to count all documents, use an empty filter.

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.collections.exceptions.TooManyDocumentsToCountException;

public class Example {
  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    // Count documents
    try {
      Integer result = collection.countDocuments(500);
      System.out.println(result);
    } catch (TooManyDocumentsToCountException error) {
      System.out.println("Number of documents exceeds upper bound or API limit");
    }
  }
}

Client reference

For more information, see the client reference.

Was this helpful?

Give Feedback

How can we improve the documentation?

© Copyright IBM Corporation 2026 | Privacy policy | Terms of use Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: Contact IBM