Search.../

having( )

Filters out groups from being returned from an aggregation.

Description

The having() function refines a WixDataAggregate so that it only includes groups from the aggregate's grouping which match the specified filter criteria.

To create a filter, use the wix-data filter() function.

Filtering using having() takes place after grouping is performed on the aggregation. To filter items before grouping, use the filter() function.

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

Syntax

function having(filter: WixDataFilter): WixDataAggregate

having Parameters

NAME
TYPE
DESCRIPTION
filter

The filter to use to filter out groups from being returned from the aggregation.

Returns

A WixDataAggregate object representing the refined aggregation.

Return Type:

Was this helpful?

Filter out groups in an aggregation

Copy Code
1let having = wixData.filter().gt("maxPopulation", 1000000);
2
3let newAggregate = aggregate.having(having);
Filter out groups in an aggregation and run the aggregation

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5let having = wixData.filter().gt("maxPopulation", 1000000);
6
7wixData.aggregate("PopulationData")
8 .group("city", "state")
9 .max("population", "maxPopulation")
10 .having(having)
11 .run()
12 .then((results) => {
13 if (results.items.length > 0) {
14 let items = results.items; // see below
15 let numItems = results.length; // 3
16 let hasNext = results.hasNext(); // false
17 } else {
18 // handle case where no matching items found
19 }
20 })
21 .catch((error) => {
22 let errorMsg = error.message;
23 let code = error.code;
24 });
25
26/* Given the sample data above, items is:
27 * [
28 * {
29 * "_id": {"city": "San Diego", "state": "CA"},
30 * "maxPopulation": 1306000,
31 * "city": "San Diego",
32 * "state": "CA"
33 * }, {
34 * "_id": {"city": "New York", "state": "NY"},
35 * "maxPopulation": 8192000,
36 * "city": "New York",
37 * "state": "NY"
38 * }, {
39 * "_id": {"city": "Los Angeles", "state": "CA"},
40 * "maxPopulation": 3796000,
41 * "city": "Los Angeles",
42 * "state": "CA"
43 * }
44 * ]
45 */
Create an aggregation with filtering and grouping 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 });