Search.../

count( )

Returns the number of items that match the query.

Description

The count() function returns a Promise that resolves to the number of items that match the query. The Promise is rejected if count() is called with incorrect permissions or if any of the functions used to refine the query is invalid.

Calling the count() function triggers the beforeCount() and afterCount() hooks if they have been defined.

Use the options parameter to run count() without checking for permissions or without its registered hooks.

Any function that does not filter query results (e.g., ascending()) does not affect the result of count().

If you build a query and don't refine it with any WixDataQuery functions, count() returns the total number of items in the collection.

If you have already run a query with find(), you can retrieve the number of query results without calling count(). The find() function returns a Promise that resolves to a WixDataQueryResult object, which has a totalCount property whose value is the number of results.

Syntax

function count([options: WixDataOptions]): Promise<number>

count Parameters

NAME
TYPE
DESCRIPTION
options
Optional
WixDataOptions

An object containing options to use when processing this operation.

Returns

Fulfilled - The number of items that match the query. Rejected - The errors that caused the rejection.

Return Type:

Promise<number>

Was this helpful?

Perform a count on a query

Copy Code
1query.count()
2 .then((num) => {
3 let numberOfItems = num;
4 })
5 .catch((error) => {
6 let errorMsg = error.message;
7 let code = error.code;
8 });
Create a query and perform a count on it

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5wixData.query("myCollection")
6 .count()
7 .then((num) => {
8 let numberOfItems = num;
9 })
10 .catch((error) => {
11 let errorMsg = error.message;
12 let code = error.code;
13 });
Create a query and perform a count on it

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5wixData.query("myCollection")
6 .eq("status", "active")
7 .gt("age", 25)
8 .count()
9 .then((num) => {
10 let numberOfItems = num;
11 })
12 .catch((error) => {
13 let errorMsg = error.message;
14 let code = error.code;
15 });