Count documents (TypeScript)
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. If you are new to the Data API, check out the quickstart. |
Result
Returns a promise that resolves to an integer indicating the exact number of documents that match the specified filter.
If the count exceeds the caller-provided or API-set upper bound, the client raises TooManyDocumentsToCountError.
Parameters
Use the countDocuments method, which belongs to the Collection class.
Method signature
async countDocuments(
filter: CollectionFilter<Schema>,
upperBound: number,
options?: {
timeout?: number | TimeoutDescriptor,
},
): number
| 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 (TypeScript). 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. |
|
|
|
The maximum number of documents to count. If the actual number of documents exceeds this value or exceeds the limit set by the Data API, the client will raise an error. |
|
|
Optional.
The options for this operation. See Properties of |
| Name | Type | Summary |
|---|---|---|
|
|
Optional. The timeout(s) to apply to this method.
You can specify For more information about the |
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 (TypeScript).
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.
import {
DataAPIClient,
TooManyDocumentsToCountError,
} 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 () {
try {
// Count documents
const result = await collection.countDocuments(
{
$and: [{ is_checked_out: false }, { number_of_pages: { $lt: 300 } }],
},
500,
);
console.log(result);
} catch (error) {
if (error instanceof TooManyDocumentsToCountError) {
console.log("Number of documents exceeds upper bound or API limit");
} else {
throw error;
}
}
})();
Count all documents
To attempt to count all documents, use an empty filter.
import {
DataAPIClient,
TooManyDocumentsToCountError,
} 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 () {
try {
// Count documents
const result = await collection.countDocuments({}, 500);
console.log(result);
} catch (error) {
if (error instanceof TooManyDocumentsToCountError) {
console.log("Number of documents exceeds upper bound or API limit");
} else {
throw error;
}
}
})();
Client reference
For more information, see the client reference.