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
-
Go
-
Java
-
C#
-
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.
|
The Go client is in preview. For more information, see astra-db-go. |
Returns 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.
Returns an integer 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
-
Go
-
Java
-
C#
-
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
|
|
The Go client is in preview. For more information, see astra-db-go. |
Use the EstimatedDocumentCount method, which belongs to the Collection type.
Method signature
func (c *Collection) EstimatedDocumentCount(
ctx context.Context,
opts ...options.CollectionEstimatedDocumentCountOption
) (int, error)
| Name | Type | Summary |
|---|---|---|
|
|
The context for the operation. |
|
Optional.
A builder to generate options for this operation.
See Methods of the |
| Method | Summary |
|---|---|
|
Optional. General API options for this operation, including the timeout. |
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.
Use the EstimateDocumentCountAsync method, which belongs to the Collection class.
You can also use EstimateDocumentCount, which is the synchronous version of the method.
Method signature
public Task<int> EstimateDocumentCountAsync(
CollectionEstimateDocumentCountOptions options = null
);
| Name | Type | Summary |
|---|---|---|
|
Optional. General API options for this operation, including the timeout. For more information and examples, see Customize API interaction. |
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
-
Go
-
Java
-
C#
-
curl
from astrapy import DataAPIClient
# Get an existing collection
client = DataAPIClient()
database = client.get_database(
"API_ENDPOINT", token="APPLICATION_TOKEN"
)
collection = database.get_collection("COLLECTION_NAME")
# Estimate count
result = collection.estimated_document_count()
print(result)
import { DataAPIClient } from "@datastax/astra-db-ts";
// Get an existing collection
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
token: "APPLICATION_TOKEN",
});
const collection = database.collection("COLLECTION_NAME");
(async function () {
// Estimate count
const result = await collection.estimatedDocumentCount();
console.log(result);
})();
|
The Go client is in preview. For more information, see astra-db-go. |
package main
import (
"context"
"fmt"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
// Get an existing collection
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
collection := database.Collection("COLLECTION_NAME")
ctx := context.Background()
// Count documents
count, err := collection.EstimatedDocumentCount(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Println(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);
}
}
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing collection
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
var collection = database.GetCollection("COLLECTION_NAME");
// Estimate count
var result = await collection.EstimateDocumentCountAsync();
Console.WriteLine(result);
}
}
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": {}
}'
Client reference
-
Python
-
TypeScript
-
Go
-
Java
-
C#
-
curl
For more information, see the client reference.
For more information, see the client reference.
|
The Go client is in preview. For more information, see astra-db-go. |
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.