Search.../

max( )

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

Description

The max() function refines a WixDataAggregate to contain the maximum 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: "propertyNameMax", where propertyName is the name of the specified property.
  • The value is the maximum value found in the specified property.

The following types of properties can be compared to determine a maximum value:

  • Number: Compares numerically.
  • Date and Time: Compares JavaScript Date objects.
  • Text: Compares lexicographically, so "text" is greater than "Text".
  • Rich Text: Compares HTML source as text.
  • URL: Compares as text.
  • Image: Compares image source as text.
  • Video: Compares video source as text.
  • Document: Compares document source as text.
  • Reference: Compares by the ID of the referenced item as a String.

Note: Aggregations can only be used on collections you have created. They cannot be used on Wix App Collections.

Syntax

function max(propertyName: string, [projectedName: string]): WixDataAggregate

max Parameters

NAME
TYPE
DESCRIPTION
propertyName
string

The property in which to find the maximum value.

projectedName
Optional
string

The name of the property in the aggregation results containing the maximum value.

Returns

A WixDataAggregate object representing the refined aggregation.

Return Type:

Was this helpful?

Add a max aggregation

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

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

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": "CA", "year": 2000},
39 * "populationMax": 3703000,
40 * "state": "CA",
41 * "year": 2000
42 * },
43 * {
44 * "_id": {"state": "FL", "year": 2010},
45 * "populationMax": 401000,
46 * "state": "FL",
47 * "year": 2010
48 * },
49 * {
50 * "_id": {"state": "CA", "year": 2010},
51 * "populationMax": 3796000,
52 * "state": "CA",
53 * "year": 2010
54 * },
55 * {
56 * "_id": {"state": "NY", "year": 2010},
57 * "populationMax": 8192000,
58 * "state": "NY",
59 * "year": 2010
60 * }
61 * ]
62 */
Create an aggregation, add a max 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 .max("population", "maximumPopulation")
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 * "maximumPopulation": 8015000,
28 * "state": "NY",
29 * "year": 2000
30 * },
31 * {
32 * "_id": {"state": "FL", "year": 2000},
33 * "maximumPopulation": 362000,
34 * "state": "FL",
35 * "year": 2000
36 * },
37 * {
38 * "_id": {"state": "CA", "year": 2000},
39 * "maximumPopulation": 3703000,
40 * "state": "CA",
41 * "year": 2000
42 * },
43 * {
44 * "_id": {"state": "FL", "year": 2010},
45 * "maximumPopulation": 401000,
46 * "state": "FL",
47 * "year": 2010
48 * },
49 * {
50 * "_id": {"state": "CA", "year": 2010},
51 * "maximumPopulation": 3796000,
52 * "state": "CA",
53 * "year": 2010
54 * },
55 * {
56 * "_id": {"state": "NY", "year": 2010},
57 * "maximumPopulation": 8192000,
58 * "state": "NY",
59 * "year": 2010
60 * }
61 * ]
62 */