> Portal Navigation: > > - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version. > - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages). > - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`). > - Top-level index of all portals: https://dev.wix.com/docs/llms.txt > - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt # Method name: avg(propertyName: string, projectedName: string) # Method package: wixData # Method menu location: wixData --> WixDataAggregate --> avg # Method Link: https://dev.wix.com/docs/velo/apis/wix-data/wix-data-aggregate/avg.md # Method Description: Refines a `WixDataAggregate` to only contain the average value from each aggregation group. The `avg()` function refines a `WixDataAggregate` to contain the average value from the specified property for each aggregated group or from the whole collection if no group is defined. When the aggregation is [run](#run), the returned `WixDataAggregateResult` object contains an item for each group with the following `key:value` pairs: + If a value was passed for the optional `projectedName`, the key is named using that value. Otherwise, the key is named using the following format: `"propertyNameAvg"`, where `propertyName` is the name of the specified property. + The value is the average of the values found in the specified property. Averages can only be calculated on properties of type Number. > **Note:** Aggregations can only be used on collections you have created. They cannot be used on [Wix App Collections](https://support.wix.com/en/article/cms-formerly-content-manager-working-with-wix-app-collections). # Method Code Examples: *** Note: do not assume any prop names or enum values other than the ones in the example. ## Add an avg aggregation ```javascript let newAggregate = aggregate.avg("population"); ``` ## Add an avg aggregation with a projected name for the results ```javascript let newAggregate = aggregate.max("population", "avgPopulation"); ``` ## Create an aggregation, add an avg aggregation, and run it ```javascript import wixData from 'wix-data'; // ... wixData.aggregate("PopulationData") .group("state", "year") .avg("population") .run() .then((results) => { let items = results.items; // see below let numItems = results.length; // 6 let hasNext = results.hasNext(); // false }) .catch((error) => { let errorMsg = error.message; let code = error.code; }); /* Given the sample data above, items is: * [ * { * "_id": {"state": "NY", "year": 2000}, * "populationAvg": 4153500, * "state": "NY", * "year": 2000}, * { * "_id": {"state": "FL", "year": 2000}, * "populationAvg": 278500, * "state": "FL", * "year": 2000 * }, * { * "_id": {"state": "CA", "year": 2000}, * "populationAvg": 2240000, * "state": "CA", * "year": 2000 * }, * { * "_id": {"state": "FL", "year": 2010}, * "populationAvg": 320500, * "state": "FL", * "year": 2010 * }, * { * "_id": {"state": "CA", "year": 2010}, * "populationAvg": 2300500, * "state": "CA", * "year": 2010 * }, * { * "_id": {"state": "NY", "year": 2010}, * "populationAvg": 4226500, * "state": "NY", * "year": 2010 * } * ] */ ``` ## Create an aggregation, add an avg aggregation with a projected name for the results, and run it ```javascript import wixData from 'wix-data'; // ... wixData.aggregate("PopulationData") .group("state", "year") .avg("population", "averagePopulation") .run() .then((results) => { let items = results.items; // see below let numItems = results.length; // 6 let hasNext = results.hasNext(); // false }) .catch((error) => { let errorMsg = error.message; let code = error.code; }); /* Given the sample data above, items is: * [ * { * "_id": {"state": "NY", "year": 2000}, * "averagePopulation": 4153500, * "state": "NY", * "year": 2000}, * { * "_id": {"state": "FL", "year": 2000}, * "averagePopulation": 278500, * "state": "FL", * "year": 2000 * }, * { * "_id": {"state": "CA", "year": 2000}, * "averagePopulation": 2240000, * "state": "CA", * "year": 2000 * }, * { * "_id": {"state": "FL", "year": 2010}, * "averagePopulation": 320500, * "state": "FL", * "year": 2010 * }, * { * "_id": {"state": "CA", "year": 2010}, * "averagePopulation": 2300500, * "state": "CA", * "year": 2010 * }, * { * "_id": {"state": "NY", "year": 2010}, * "averagePopulation": 4226500, * "state": "NY", * "year": 2010 * } * ] */ ``` ---