Search.../

avg( )

Refines a WixDataAggregate to only contain the average value from each aggregation group.

Description

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, 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.

Authorization

Request

This endpoint does not take any parameters

Response Object

A WixDataAggregate object representing the refined aggregation.

Returns an empty object.

Status/Error Codes

Was this helpful?

Add an avg aggregation

Copy Code
1let newAggregate = aggregate.avg("population");
Add an avg aggregation with a projected name for the results

Copy Code
1let newAggregate = aggregate.max("population", "avgPopulation");
Create an aggregation, add an avg aggregation, and run it

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5wixData.aggregate("PopulationData")
6 .group("state", "year")
7 .avg("population")
8 .run()
9 .then((results) => {
10 let items = results.items; // see below
11 let numItems = results.length; // 6
12 let hasNext = results.hasNext(); // false
13 })
14 .catch((error) => {
15 let errorMsg = error.message;
16 let code = error.code;
17 });
18
19/* Given the sample data above, items is:
20 * [
21 * {
22 * "_id": {"state": "NY", "year": 2000},
23 * "populationAvg": 4153500,
24 * "state": "NY",
25 * "year": 2000},
26 * {
27 * "_id": {"state": "FL", "year": 2000},
28 * "populationAvg": 278500,
29 * "state": "FL",
30 * "year": 2000
31 * },
32 * {
33 * "_id": {"state": "CA", "year": 2000},
34 * "populationAvg": 2240000,
35 * "state": "CA",
36 * "year": 2000
37 * },
38 * {
39 * "_id": {"state": "FL", "year": 2010},
40 * "populationAvg": 320500,
41 * "state": "FL",
42 * "year": 2010
43 * },
44 * {
45 * "_id": {"state": "CA", "year": 2010},
46 * "populationAvg": 2300500,
47 * "state": "CA",
48 * "year": 2010
49 * },
50 * {
51 * "_id": {"state": "NY", "year": 2010},
52 * "populationAvg": 4226500,
53 * "state": "NY",
54 * "year": 2010
55 * }
56 * ]
57 */
Create an aggregation, add an avg aggregation with a projected name for the results, and run it

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5wixData.aggregate("PopulationData")
6 .group("state", "year")
7 .avg("population", "averagePopulation")
8 .run()
9 .then((results) => {
10 let items = results.items; // see below
11 let numItems = results.length; // 6
12 let hasNext = results.hasNext(); // false
13 })
14 .catch((error) => {
15 let errorMsg = error.message;
16 let code = error.code;
17 });
18
19/* Given the sample data above, items is:
20 * [
21 * {
22 * "_id": {"state": "NY", "year": 2000},
23 * "averagePopulation": 4153500,
24 * "state": "NY",
25 * "year": 2000},
26 * {
27 * "_id": {"state": "FL", "year": 2000},
28 * "averagePopulation": 278500,
29 * "state": "FL",
30 * "year": 2000
31 * },
32 * {
33 * "_id": {"state": "CA", "year": 2000},
34 * "averagePopulation": 2240000,
35 * "state": "CA",
36 * "year": 2000
37 * },
38 * {
39 * "_id": {"state": "FL", "year": 2010},
40 * "averagePopulation": 320500,
41 * "state": "FL",
42 * "year": 2010
43 * },
44 * {
45 * "_id": {"state": "CA", "year": 2010},
46 * "averagePopulation": 2300500,
47 * "state": "CA",
48 * "year": 2010
49 * },
50 * {
51 * "_id": {"state": "NY", "year": 2010},
52 * "averagePopulation": 4226500,
53 * "state": "NY",
54 * "year": 2010
55 * }
56 * ]
57 */