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.
Note: Aggregations can only be used on collections you have created. They cannot be used on Wix App Collections.
Syntax
function group(propertyName: ...string): WixDataAggregate
group Parameters
NAME
TYPE
DESCRIPTION
propertyName
string
The property or properties to group on.
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';23// ...45wixData.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 below12 let numItems = results.length; // 313 let hasNext = results.hasNext(); // false14 } else {15 // handle case where no matching items found16 }17 })18 .catch((error) => {19 let errorMsg = error.message;20 let code = error.code;21 });2223/* 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';23// ...45wixData.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 below12 let numItems = results.length; // 613 let hasNext = results.hasNext(); // false14 } else {15 // handle case where no matching items found16 }17 })18 .catch((error) => {19 let errorMsg = error.message;20 let code = error.code;21 });2223/* Given the sample data above, items is:24 * [25 * {26 * "_id": {"state": "NY", "year": 2000},27 * "populationMax": 8015000,28 * "state": "NY",29 * "year": 200030 * },31 * {32 * "_id": {"state": "FL", "year": 2000},33 * "populationMax": 362000,34 * "state": "FL",35 * "year": 200036 * },37 * {38 * "_id": {"state": "NY", "year": 2010},39 * "populationMax": 8192000,40 * "state": "NY",41 * "year": 201042 * },43 * {44 * "_id": {"state": "CA", "year": 2010},45 * "populationMax": 3796000,46 * "state": "CA",47 * "year": 201048 * },49 * {50 * "_id": {"state": "CA", "year": 2000},51 * "populationMax": 3703000,52 * "state": "CA",53 * "year": 200054 * },55 * {56 * "_id": {"state": "FL", "year": 2010},57 * "populationMax": 401000,58 * "state": "FL",59 * "year": 201060 * }61 * ]62 */
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 });