Estimate document count (Java)
Estimates the number of documents in a collection.
This method only returns an estimated count. It is an alternative to getting an exact count, which is a slow and expensive operation.
The estimated count is based on current system statistics at the time the request is received by the database server.
|
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 a long indicating the server-side estimate of the total number of documents in the collection.
Parameters
Use the estimatedDocumentCount method, which belongs to the com.datastax.astra.client.Collection class.
Method signature
long estimatedDocumentCount()
long estimatedDocumentCount(EstimatedCountDocumentsOptions options)
EstimatedCountDocumentsOptions doesn’t include any method-specific options.
Examples
The following example demonstrates how to estimate the number of documents in a collection.
Estimate document count
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.definition.documents.Document;
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");
// Estimate count
long result = collection.estimatedDocumentCount();
System.out.println(result);
}
}
Client reference
For more information, see the client reference.