Count documents (HTTP)
|
For details about additional languages, see: |
Counts documents in a collection using filter clauses.
Exact counting of the number of documents is a slow, expensive operation. If the document count exceeds the upper limit set by you or by the API, an indication of this limit will be returned instead of the full count.
If you need to count large numbers of documents, consider using the method to estimate count or the DataStax Bulk Loader instead.
|
Ready to write code? See the examples for this method to get started. |
Result
The response includes a status.count property, which is an integer indicating the exact number of documents that match the specified filter.
If the count exceeds the upper bound set by the API, then the status.count value will be the upper bound, and the status.moreData value is true.
Example response:
{
"status": {
"count": 105
}
}
Example response if the number of documents exceeds the upper bound.
{
"status": {
"moreData": true,
"count": 1000
}
}
Parameters
Use the countDocuments command.
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 '{
"countDocuments": {
"filter": FILTER
}
}'
| Name | Type | Summary |
|---|---|---|
|
|
An object that defines filter criteria using the Data API filter syntax. The method only finds documents that match the filter criteria. Filters can improve performance by reducing the number of documents that the Data API processes. You must use For a list of available filter operators and more examples, see Filter operators for collections (HTTP). Filters can use only indexed fields. If you apply selective indexing when you create a collection, you cannot reference non-indexed fields in a filter. For an example, see Count documents that match a filter. |
Examples
The following examples demonstrate how to count documents in a collection.
Count documents that match a filter
You can use a filter to count documents that match specific criteria.
For a list of available filter operators and more examples, see Filter operators for collections (HTTP).
Filters can use only indexed fields. If you apply selective indexing when you create a collection, you cannot reference non-indexed fields in a filter.
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"countDocuments": {
"filter": {"$and": [
{"is_checked_out": false},
{"number_of_pages": {"$lt": 300}}
]}
}
}'
Count all documents
To attempt to count all documents, use an empty filter.
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"countDocuments": {
"filter": {}
}
}'