Search...
filter( )
Filters out items from being used in an aggregation.
Description
The filter()
function refines a WixDataAggregate
so that it only
includes items from the aggregate's collection which match the specified
filter criteria.
To create a filter, use the wix-data filter()
function.
Filtering using filter()
takes place before grouping is performed on the
aggregation. To filter grouped results, use the having()
function.
Note: Aggregations can only be used on collections you have created. They cannot be used on Wix App Collections.
Syntax
function filter(filter: WixDataFilter): WixDataAggregate
filter Parameters
NAME
TYPE
DESCRIPTION
Was this helpful?
Filter out items in an aggregation
Copy Code
1let filter = wixData.filter().eq("year", 2010);23let newAggregate = aggregate.filter(filter);
Filter out items in an aggregation and run the aggregation
Copy Code
1import wixData from 'wix-data';23// ...45let filter = wixData.filter().eq("year", 2010);67wixData.aggregate("PopulationData")8 .filter(filter)9 .max("population", "maxPopulation")10 .run()11 .then((results) => {12 if (results.items.length > 0) {13 let items = results.items; // see below14 let numItems = results.length; // 115 let hasNext = results.hasNext(); // false16 } else {17 // handle case where no matching items found18 }19 })20 .catch((error) => {21 let errorMsg = error.message;22 let code = error.code;23 });2425// items is: [{"_id":"0","maxPopulation":8192000}]
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 });