> 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: max(propertyName: string, projectedName: string) # Method package: wixData # Method menu location: wixData --> WixDataAggregate --> max # Method Link: https://dev.wix.com/docs/velo/apis/wix-data/wix-data-aggregate/max.md # Method Description: Refines a `WixDataAggregate` to only contain the maximum value from each aggregation group. 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()](wix-data.WixDataAggregate.html#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](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 a max aggregation ```javascript let newAggregate = aggregate.max("population"); ``` ## Add a max aggregation with a projected name for the results ```javascript let newAggregate = aggregate.max("population", "maxPopulation"); ``` ## Create an aggregation, add a max aggregation, and run it ```javascript import wixData from 'wix-data'; // ... wixData.aggregate("PopulationData") .group("state", "year") .max("population") .run() .then((results) => { if (results.items.length > 0) { let items = results.items; // see below let numItems = results.length; // 6 let hasNext = results.hasNext(); // false } else { // handle case where no matching items found } }) .catch((error) => { let errorMsg = error.message; let code = error.code; }); /* Given the sample data above, items is: * [ * { * "_id": {"state": "NY", "year": 2000}, * "populationMax": 8015000, * "state": "NY", * "year": 2000 * }, * { * "_id": {"state": "FL", "year": 2000}, * "populationMax": 362000, * "state": "FL", * "year": 2000 * }, * { * "_id": {"state": "CA", "year": 2000}, * "populationMax": 3703000, * "state": "CA", * "year": 2000 * }, * { * "_id": {"state": "FL", "year": 2010}, * "populationMax": 401000, * "state": "FL", * "year": 2010 * }, * { * "_id": {"state": "CA", "year": 2010}, * "populationMax": 3796000, * "state": "CA", * "year": 2010 * }, * { * "_id": {"state": "NY", "year": 2010}, * "populationMax": 8192000, * "state": "NY", * "year": 2010 * } * ] */ ``` ## Create an aggregation, add a max aggregation with a projected name for the results, and run it ```javascript import wixData from 'wix-data'; // ... wixData.aggregate("PopulationData") .group("state", "year") .max("population", "maximumPopulation") .run() .then((results) => { if (results.items.length > 0) { let items = results.items; // see below let numItems = results.length; // 6 let hasNext = results.hasNext(); // false } else { // handle case where no matching items found } }) .catch((error) => { let errorMsg = error.message; let code = error.code; }); /* Given the sample data above, items is: * [ * { * "_id": {"state": "NY", "year": 2000}, * "maximumPopulation": 8015000, * "state": "NY", * "year": 2000 * }, * { * "_id": {"state": "FL", "year": 2000}, * "maximumPopulation": 362000, * "state": "FL", * "year": 2000 * }, * { * "_id": {"state": "CA", "year": 2000}, * "maximumPopulation": 3703000, * "state": "CA", * "year": 2000 * }, * { * "_id": {"state": "FL", "year": 2010}, * "maximumPopulation": 401000, * "state": "FL", * "year": 2010 * }, * { * "_id": {"state": "CA", "year": 2010}, * "maximumPopulation": 3796000, * "state": "CA", * "year": 2010 * }, * { * "_id": {"state": "NY", "year": 2010}, * "maximumPopulation": 8192000, * "state": "NY", * "year": 2010 * } * ] */ ``` ---