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.

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 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';
2
3// ...
4
5wixData.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 below
13 let numItems = results.length; // 3
14 let hasNext = results.hasNext(); // true
15 } else {
16 // handle case where no matching items found
17 }
18 })
19 .catch((error) => {
20 let errorMsg = error.message;
21 let code = error.code;
22 });
23
24/* Given the sample data above, items is:
25 * [
26 * {
27 * "_id": {"state": "NY", "year": 2000},
28 * "populationMax":8015000,
29 * "state": "NY",
30 * "year": 2000
31 * }, {
32 * "_id": {"state": "FL", "year": 2000},
33 * "populationMax":362000,
34 * "state": "FL",
35 * "year": 2000
36 * }, {
37 * "_id": {"state": "NY", "year": 2010},
38 * "populationMax":3703000,
39 * "state": "NY",
40 * "year": 2010
41 * }
42 * ]
43 */
Create an aggregation with filtering and grouping and run it

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5const filter = wixData.filter().eq("year", 2010);
6const having = wixData.filter().gt("maxPopulation", 1000000);
7
8wixData.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 found
24 }
25 })
26 .catch((error) => {
27 let errorMsg = error.message;
28 let code = error.code;
29 });