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
filter

The filter to use to filter out items from being used in the aggregation.

Returns

A WixDataAggregate object representing the refined aggregation.

Return Type:

Was this helpful?

Filter out items in an aggregation

Copy Code
1let filter = wixData.filter().eq("year", 2010);
2
3let newAggregate = aggregate.filter(filter);
Filter out items in an aggregation and run the aggregation

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5let filter = wixData.filter().eq("year", 2010);
6
7wixData.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 below
14 let numItems = results.length; // 1
15 let hasNext = results.hasNext(); // false
16 } else {
17 // handle case where no matching items found
18 }
19 })
20 .catch((error) => {
21 let errorMsg = error.message;
22 let code = error.code;
23 });
24
25// items is: [{"_id":"0","maxPopulation":8192000}]
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 });