Search.../

group( )

Groups items together in an aggregation.

Description

The group() function refines a WixDataAggregate so that its items are grouped by the specified property or properties.

You can perform aggregations on the groups using the following functions:

To filter grouped results, use the having() function.

Notes:

  • Aggregations can only be used on collections you have created. They cannot be used on Wix App Collections.
  • You can only call the group() function once per aggregate query.

Syntax

function group(propertyName: ...string): WixDataAggregate

group Parameters

NAME
TYPE
DESCRIPTION
propertyName
string

The property or properties to group on.

Returns

A WixDataAggregate object representing the refined aggregation.

Return Type:

Was this helpful?

Group items in an aggregation

Copy Code
1let newAggregate = aggregate.group("state");
Group items by multiple fields in an aggregation

Copy Code
1let newAggregate = aggregate.group("state", "year");
Group items in an aggregation and run the aggregation

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5wixData.aggregate("PopulationData")
6 .group("state")
7 .max("population")
8 .run()
9 .then((results) => {
10 if (results.items.length > 0) {
11 let items = results.items; // see below
12 let numItems = results.length; // 3
13 let hasNext = results.hasNext(); // false
14 } else {
15 // handle case where no matching items found
16 }
17 })
18 .catch((error) => {
19 let errorMsg = error.message;
20 let code = error.code;
21 });
22
23/* Given the sample data above, items is:
24 * [
25 * {"_id": "FL", "populationMax": 401000},
26 * {"_id": "CA", "populationMax": 3796000},
27 * {"_id": "NY", "populationMax": 8192000}
28 * ]
29 */
Group items in an aggregation and run the aggregation

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5wixData.aggregate("PopulationData")
6 .group("state", "year")
7 .max("population")
8 .run()
9 .then((results) => {
10 if (results.items.length > 0) {
11 let items = results.items; // see below
12 let numItems = results.length; // 6
13 let hasNext = results.hasNext(); // false
14 } else {
15 // handle case where no matching items found
16 }
17 })
18 .catch((error) => {
19 let errorMsg = error.message;
20 let code = error.code;
21 });
22
23/* Given the sample data above, items is:
24 * [
25 * {
26 * "_id": {"state": "NY", "year": 2000},
27 * "populationMax": 8015000,
28 * "state": "NY",
29 * "year": 2000
30 * },
31 * {
32 * "_id": {"state": "FL", "year": 2000},
33 * "populationMax": 362000,
34 * "state": "FL",
35 * "year": 2000
36 * },
37 * {
38 * "_id": {"state": "NY", "year": 2010},
39 * "populationMax": 8192000,
40 * "state": "NY",
41 * "year": 2010
42 * },
43 * {
44 * "_id": {"state": "CA", "year": 2010},
45 * "populationMax": 3796000,
46 * "state": "CA",
47 * "year": 2010
48 * },
49 * {
50 * "_id": {"state": "CA", "year": 2000},
51 * "populationMax": 3703000,
52 * "state": "CA",
53 * "year": 2000
54 * },
55 * {
56 * "_id": {"state": "FL", "year": 2010},
57 * "populationMax": 401000,
58 * "state": "FL",
59 * "year": 2010
60 * }
61 * ]
62 */
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 });