Search.../

aggregate( )

Creates an aggregation.

Description

The aggregate() function builds an aggregation on the specified collection and returns a WixDataAggregate object.

The returned object contains the aggregate definition which is typically used to run the aggregation using the run() function.

You can refine the aggregation by chaining WixDataAggregate functions on to the aggregate.

The aggregate() function runs with the following WixDataAggregate defaults that you can override:

Syntax

function aggregate(collectionId: string): WixDataAggregate

aggregate Parameters

NAME
TYPE
DESCRIPTION
collectionId
string

The ID of the collection to run the aggregation on.

To find your collectionId, select the Databases tab in the Velo Sidebar. Hover over your collection, click the three dots, and select Edit Settings.

Returns

An aggregate object.

Return Type:

Was this helpful?

Perform an aggregation

Copy Code
1aggregate.run()
2 .then((results) => {
3 if (results.items.length > 0) {
4 let items = results.items;
5 let numItems = results.length;
6 let hasNext = results.hasNext();
7 } else {
8 // handle case where no matching items found
9 }
10 })
11 .catch((error) => {
12 let errorMsg = error.message;
13 let code = error.code;
14 });
Create an aggregation and run it

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5const filter = wixData.filter().eq("year", 2010);
6const having = wixData.filter().gt("maxPopulation", 1000000);
7
8wixData.aggregate("PopulationData")
9 .filter(filter)
10 .group("state")
11 .max("population", "maxPopulation")
12 .having(having)
13 .descending("maxPopulation")
14 .skip(5)
15 .limit(3)
16 .run()
17 .then((results) => {
18 if (results.items.length > 0) {
19 let items = results.items;
20 let numItems = results.length;
21 let hasNext = results.hasNext();
22 } else {
23 // handle case where no matching items found
24 }
25 })
26 .catch((error) => {
27 let errorMsg = error.message;
28 let code = error.code;
29 });