Search.../

count( )

Refines a WixDataAggregate to contain the item count of each group in the aggregation.

Description

The count() function refines a WixDataAggregate to contain the item count in each of the aggregate's groups.

When the aggregation is run, the returned WixDataAggregateResult object contains items with the following additional key:value pair:

  • The key is named "count".
  • The value is the count of items aggregated in the group.

Note: Aggregations can only be used on collections you have created. They cannot be used on Wix App Collections.

Syntax

function count(): WixDataAggregate

count Parameters

This function does not take any parameters.

Returns

A WixDataAggregate object representing the refined aggregation.

Return Type:

Was this helpful?

Add a sum aggregation

Code Example

Copy Code
1let newAggregate = aggregate.count();
Create an aggregation, add a sum aggregation, and run it

Code Example

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5wixData.aggregate("PopulationData")
6 .group("state", "year")
7 .count()
8 .run()
9 .then( (results) => {
10 if (results.items.length > 0) {
11 let items = results.items; // see below
12 let numItems = results.length; // 6
13 let hasNext = results.hasNext(); // false
14 } else {
15 // handle case where no matching items found
16 }
17 } )
18 .catch( (error) => {
19 let errorMsg = error.message;
20 let code = error.code;
21 } );
22
23/* Given the sample data above, items is:
24 * [
25 * {
26 * "_id": {"state": "NY", "year": 2000},
27 * "count": 2,
28 * "state": "NY",
29 * "year": 2000
30 * },
31 * {
32 * "_id": {"state": "FL", "year": 2000},
33 * "count": 2,
34 * "state": "FL",
35 * "year": 2000
36 * },
37 * {
38 * "_id": {"state": "CA", "year": 2000},
39 * "count": 2,
40 * "state": "CA",
41 * "year": 2000
42 * },
43 * {
44 * "_id": {"state": "FL", "year": 2010},
45 * "count": 2,
46 * "state": "FL",
47 * "year": 2010
48 * },
49 * {
50 * "_id": {"state": "CA", "year": 2010},
51 * "count": 2,
52 * "state": "CA",
53 * "year": 2010
54 * },
55 * {
56 * "_id": {"state": "NY", "year": 2010},
57 * "count": 2,
58 * "state": "NY",
59 * "year": 2010
60 * }
61 * ]
62 */