Estimate document count (Go)
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 an integer indicating the server-side estimate of the total number of documents in the collection.
Parameters
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. |
Examples
The following example demonstrates how to estimate the number of documents in a collection.
Estimate document count
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() {
ctx := context.Background()
// Get an existing collection
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
collection := database.Collection("COLLECTION_NAME")
// Count documents
count, err := collection.EstimatedDocumentCount(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Println(count)
}
Client reference
For more information, see the client reference.