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.
Was this helpful?
Add a sum aggregation
Copy Code
1let newAggregate = aggregate.count();
Create an aggregation, add a sum aggregation, and run it
Copy Code
1import wixData from 'wix-data';23// ...45wixData.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 below12 let numItems = results.length; // 613 let hasNext = results.hasNext(); // false14 } else {15 // handle case where no matching items found16 }17 })18 .catch((error) => {19 let errorMsg = error.message;20 let code = error.code;21 });2223/* Given the sample data above, items is:24 * [25 * {26 * "_id": {"state": "NY", "year": 2000},27 * "count": 2,28 * "state": "NY",29 * "year": 200030 * },31 * {32 * "_id": {"state": "FL", "year": 2000},33 * "count": 2,34 * "state": "FL",35 * "year": 200036 * },37 * {38 * "_id": {"state": "CA", "year": 2000},39 * "count": 2,40 * "state": "CA",41 * "year": 200042 * },43 * {44 * "_id": {"state": "FL", "year": 2010},45 * "count": 2,46 * "state": "FL",47 * "year": 201048 * },49 * {50 * "_id": {"state": "CA", "year": 2010},51 * "count": 2,52 * "state": "CA",53 * "year": 201054 * },55 * {56 * "_id": {"state": "NY", "year": 2010},57 * "count": 2,58 * "state": "NY",59 * "year": 201060 * }61 * ]62 */