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
Was this helpful?
Filter out groups in an aggregation
Copy Code
1let having = wixData.filter().gt("maxPopulation", 1000000);23let newAggregate = aggregate.having(having);
Filter out groups in an aggregation and run the aggregation
Copy Code
1import wixData from 'wix-data';23// ...45let having = wixData.filter().gt("maxPopulation", 1000000);67wixData.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 below15 let numItems = results.length; // 316 let hasNext = results.hasNext(); // false17 } else {18 // handle case where no matching items found19 }20 })21 .catch((error) => {22 let errorMsg = error.message;23 let code = error.code;24 });2526/* 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';23// ...45const filter = wixData.filter().eq("year", 2010);6const having = wixData.filter().gt("maxPopulation", 1000000);78wixData.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 found24 }25 })26 .catch((error) => {27 let errorMsg = error.message;28 let code = error.code;29 });