Note: Aggregations can only be used on collections you have created. They cannot be used on Wix App Collections.
Note: Before reading this article, you should be familiar with using the Data API. To learn more, see Working with the Data API.
Sample Data
represents population statistics for cities collected over multiple years. In our examples, we assume the data is contained in a collection named PopulationData.
- Create a collection on your site named PopulationData.
- Save the above data in a .csv file.
- Import the .csv file into the PopulationData collection.
- Copy and paste the code snippets found below into the code panel on one of your sites pages.
Importing wix-data
wix-data
.import wixData from 'wix-data';
Running Aggregations
- Create an aggregation using the
function.aggregate()
- Refine the aggregation using the functions described below.
- Run the aggregation using the
function.run()
- Handle the aggregation results.
wixData.aggregate("PopulationData")
.max("population")
.run()
.then( (results) => {
let populationMax = results.items[0].populationMax;
} );
- Sets the number of items or groups to skip before returning aggregation results.skip()
- Limits the number of items or groups the aggregation returns.limit()
Aggregation Results
aggregate()
function's Promise:
- An array of the aggregated items or groups. Each value is contained in an object that is an element of the array. The structure of the objects depends on which aggregations have been run.items
- The number of items or groups in the aggregate results.length
- Indicates if the aggregation has more results. Aggregation results are paged. So if your aggregation returns more results than the page size, you will have multiple pages of results.hasNext()
- Retrieves the next page of aggregate results.next()
Aggregations Structure
import wixData from 'wix-data';
// ...
const filter = wixData.filter().eq("year", 2010);
const having = wixData.filter().gt("maxPopulation", 1000000);
wixData.aggregate("PopulationData")
.filter(filter)
.group("state")
.max("population", "maxPopulation")
.having(having)
.descending("maxPopulation")
.run()
.then( (results) => {
console.log(results.items);
console.log(results.length);
console.log(results.hasNext());
} )
.catch( (error) => {
console.log(error.message);
console.log(error.code);
} );
filter()
function to narrow down which items are included in an aggregation.year
is not 2010
.filter()
function takes a WixDataFilter
object created using the wix-data.filter()
function (line 5 above). Use any of the WixDataFilter
filtering functions to build your WixDataFilter
object.group()
function to group retrieved items together and then optionally calculate aggregated values and further filter the groups.define each group.
max()
function is used to get the largest population value from each of the state groups.group()
function can also be used to create groups based on multiple fields.having()
function to narrow down which groups are included in an aggregation. The having()
function differs from filter in that it is applied after the groupings are made. So filter()
filters out items from the collection that you don't want considered at all while having()
filters out groups that don't match the given criteria.maxPopulation
is less or equal to 1000000
.having()
function takes a WixDataFilter
object created using the wix-data.filter()
function (line 6 above). Use any of the WixDataFilter
filtering functions to build your WixDataFilter
object.ascending()
and decending()
functions to sort the aggregation's resulting items or groups.maxPopulation
values.that are created as part of the grouping and aggregation process. For
example, the sort on line 13 above is performed on the
maxPopulation
field, which is not a field in the collection, but a field created by the max()
aggregation.Examples
populationMax
" because we are calling the max()
function and passing it the "population
" field key.wixData.aggregate("PopulationData")
.max("population")
.run()
.then( (results) => {
let items = results.items;
} );
/* items is:
* [{"_id": "0", "populationMax": 8192000}]
*/
wixData.aggregate("PopulationData")
.group("state")
.max("population")
.run()
.then( (results) => {
let items = results.items;
} );
/* items is:
* [
* {"_id": "FL", "populationMax": 401000},
* {"_id": "CA", "populationMax": 3796000},
* {"_id": "NY", "populationMax": 8192000}
* ]
*/
wixData.aggregate("PopulationData")
.group("state")
.count()
.run()
.then( (results) => {
let items = results.items;
} );
/* items is:
* [
* {"_id":"FL","count":4},
* {"_id":"CA","count":6},
* {"_id":"NY","count":4}
* ]
*/
wixData.aggregate("PopulationData")
.group("state", "year")
.max("population")
.run()
.then( (results) => {
let items = results.items;
} );
/* 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
* }
* ]
*/
wixData.aggregate("PopulationData")
.group("state", "year")
.count()
.run()
.then( (results) => {
let items = results.items;
} );
/* items is:
* [
* {
* "_id": {"state": "NY", "year": 2000},
* "count": 2,
* "state": "NY",
* "year": 2000
* },{
* "_id": {"state": "FL", "year": 2000},
* "count": 2,
* "state": "FL",
* "year": 2000
* },{
* "_id": {"state": "CA", "year": 2000},
* "count": 3,
* "state": "CA",
* "year": 2000
* },{
* "_id": {"state": "FL", "year": 2010},
* "count": 2,
* "state": "FL",
* "year": 2010
* },{
* "_id": {"state": "CA", "year": 2010},
* "count": 3,
* "state": "CA",
* "year": 2010
* },{
* "_id": {"state": "NY", "year": 2010},
* "count": 2,
* "state": "NY",
* "year": 2010
* }
* ]
*/
let filter = wixData.filter().eq("year", 2000);
wixData.aggregate("PopulationData")
.filter(filter)
.max("population")
.run()
.then( (results) => {
let items = results.items;
} );
// items is: [{"_id": "0", "populationMax": 8015000}]
wixData.aggregate("PopulationData")
.group("state")
.max("population")
.ascending("populationMax")
.run()
.then( (results) => {
let items = results.items;
} );
/* items is:
* [
* {"_id": "FL", "populationMax": 401000},
* {"_id": "CA", "populationMax": 3796000},
* {"_id": "NY", "populationMax": 8192000}
* ]
*/