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.
The caller must have the SELECT permission on the system keyspace.
For more information, see Manage roles and permissions.
|
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.DataAPIClients;
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 =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getCollection("COLLECTION_NAME");
// Estimate count
long result = collection.estimatedDocumentCount();
System.out.println(result);
}
}
Client reference
For more information, see the client reference.