Search...
limit( )
Limits the number of items or groups the aggregation returns.
Description
The limit()
function defines the number of results an aggregation returns in each
page. Only one page of results is retreived at a time. The next()
function is used to navigate the pages of a query result.
By default, limit
is set to 50
.
The maximum value that limit()
can accept is 1000
.
Note: Aggregations can only be used on collections you have created. They cannot be used on Wix App Collections.
Syntax
function limit(limit: number): WixDataAggregate
limit Parameters
NAME
TYPE
DESCRIPTION
limit
number
The number of items or groups to return.
Was this helpful?
Add a limit to an aggregation
Copy Code
1let newAggregate = aggregate.limit(3);
Create an aggregate, add a limit, and run it
Copy Code
1import wixData from 'wix-data';23// ...45wixData.aggregate("PopulationData")6 .group("state", "year")7 .max("population")8 .limit(3)9 .run()10 .then((results) => {11 if (results.items.length > 0) {12 let items = results.items; // see below13 let numItems = results.length; // 314 let hasNext = results.hasNext(); // true15 } else {16 // handle case where no matching items found17 }18 })19 .catch((error) => {20 let errorMsg = error.message;21 let code = error.code;22 });2324/* Given the sample data above, items is:25 * [26 * {27 * "_id": {"state": "NY", "year": 2000},28 * "populationMax":8015000,29 * "state": "NY",30 * "year": 200031 * }, {32 * "_id": {"state": "FL", "year": 2000},33 * "populationMax":362000,34 * "state": "FL",35 * "year": 200036 * }, {37 * "_id": {"state": "NY", "year": 2010},38 * "populationMax":3703000,39 * "state": "NY",40 * "year": 201041 * }42 * ]43 */
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 });