Estimate document count
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
-
Python
-
TypeScript
-
Java
-
curl
Returns an integer indicating the server-side estimate of the total number of documents in the collection.
Returns a promise that resolves to an integer indicating the server-side estimate of the total number of documents in the collection.
Returns a long indicating the server-side estimate of the total number of documents in the collection.
The response includes a status.count property, which is an integer indicating the server-side estimate of the total number of documents in the collection.
Example response:
{
"status": {
"count": 37500
}
}
Parameters
-
Python
-
TypeScript
-
Java
-
curl
Use the estimated_document_count method, which belongs to the astrapy.Collection class.
Method signature
estimated_document_count(
*,
general_method_timeout_ms: int,
request_timeout_ms: int,
timeout_ms: int,
) -> int:
| Name | Type | Summary |
|---|---|---|
|
|
Optional. The maximum time, in milliseconds, that the client should wait for the underlying HTTP request. Default: The default value for the collection. This default is 30 seconds unless you specified a different default when you initialized the |
|
|
Optional.
An alias for |
|
|
Optional.
An alias for |
Use the estimatedDocumentCount method, which belongs to the Collection class.
Method signature
async estimatedDocumentCount(
options?: {
timeout?: number | TimeoutDescriptor,
},
): number
| Name | Type | Summary |
|---|---|---|
|
|
Optional.
The options for this operation. See Properties of |
| Name | Type | Summary |
|---|---|---|
|
|
Optional. The timeout(s) to apply to this method.
You can specify Details about the
|
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.
The estimatedDocumentCount command doesn’t accept any parameters.
Command signature
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"estimatedDocumentCount": {}
}'
Examples
The following example demonstrates how to estimate the number of documents in a collection.
Estimate document count
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
# Get an existing collection
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
collection = database.get_collection(
"COLLECTION_NAME", keyspace="KEYSPACE_NAME"
)
# Estimate count
result = collection.estimated_document_count()
print(result)
import {
DataAPIClient,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get an existing collection
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
const collection = database.collection("COLLECTION_NAME", {
keyspace: "KEYSPACE_NAME",
});
(async function () {
// Estimate count
const result = await collection.estimatedDocumentCount();
console.log(result);
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.databases.Database;
public class Example {
public static void main(String[] args) {
// Get an existing collection
DataAPIClient client = DataAPIClients.clientHCD("USERNAME", "PASSWORD");
Database database = client.getDatabase("API_ENDPOINT", "KEYSPACE_NAME");
Collection<Document> collection = database.getCollection("COLLECTION_NAME");
// Estimate count
long result = collection.estimatedDocumentCount();
System.out.println(result);
}
}
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"estimatedDocumentCount": {}
}'
Client reference
-
Python
-
TypeScript
-
Java
-
curl
For more information, see the client reference.
For more information, see the client reference.
For more information, see the client reference.
Client reference documentation is not applicable for HTTP.