CorvidReference

WixDataAggregateResult

The results of an aggregation, containing the aggregated values.

When you execute an aggregation with the run() function, it returns a Promise that resolves to a WixDataAggregateResult object, which contains the aggregated values.

Table of Contents

PROPERTIES

?
Store values associated with an object.
itemsGets the aggregated values.
lengthReturns the number of values in the aggregate results.

FUNCTIONS

?
Perform actions on an object.
hasNext( )Indicates if the aggregation has more results.
next( )Retrieves the next page of aggregate results.

items

Gets the aggregated values.

Description

The current page of items retrieved by the aggregation.

The page size is defined by the limit() function and navigating through pages is done with the next() function.

When no items match the aggregation, the items array is empty.

Syntax

get items(): Array<Object>
TYPE
?
The kind of data the property stores.
Array<Object>

Examples

Get the aggregate values

let items = results.items;

length

Returns the number of values in the aggregate results.

Syntax

get length(): number
TYPE
?
The kind of data the property stores.
number

Examples

Get the number of values in the aggregate results

let length = results.length; // 4

Run an aggregation and get the number of values in the aggregate results

import wixData from 'wix-data';

wixData.aggregate("PopulationData")
  .group("state")
  .max("population")
  .run()
  .then( (results) => {
    let length = results.length;  // 3
  } );

hasNext( )

Indicates if the aggregation has more results.

Syntax

function hasNext(): boolean
RETURN VALUE
?
Value that a function evaluates to when it is finished running.
boolean
true if there are more results.

Examples

Get whether the aggregate result object has more results

let hasNext = results.hasNext(); // true

Run an aggregation and get whether the aggregate result object has more results

import wixData from 'wix-data';

wixData.aggregate("PopulationData")
  .group("state")
  .max("population")
  .run()
  .then( (results) => {
    let hasNext = results.hasNext();  // false
  } );

Iterate through all pages of aggregate results

let results = await wixData.aggregate("Cities")
  .group("city")
  .limit(3)
  .run();

console.log(results.items);

while(results.hasNext()) {
  console.log("---▼ Next Page ▼---");
  results = await results.next();
  console.log(results.items);
}

/* Logs:
 * [{"_id":"San Diego"},{"_id":"Orlando"},{"_id":"San Francisco"}]
 * ---▼ Next Page ▼---
 * [{"_id":"Buffalo"},{"_id":"Miami"},{"_id":"Los Angeles"}]
 * ---▼ Next Page ▼---
 * [{"_id":"Miami"}]
 */

next( )

Retrieves the next page of aggregate results.

Description

The next() function retrieves the next page of aggregate results.

The page size is defined by the limit() function.

If items are added or removed between calls to next() the values returned by WixDataAggregateResult may change.

Syntax

function next(): Promise<WixDataAggregateResult>
RETURN VALUE
?
Value that a function evaluates to when it is finished running.

Fulfilled - An aggregate object with the next page of aggregate results. Rejected - The errors that caused the rejection.

Examples

Get the next page of an aggregate result

oldResults.next()
  .then( (newResults) => {
    let items = newResults.items;
    let numItems = newResults.length;
    let hasNext = newResults.hasNext();
  } )
  .catch( (error) => {
    let errorMsg = error.message;
    let code = error.code;
  } );

Iterate through all pages of aggregate results

let results = await wixData.aggregate("Cities")
  .group("city")
  .limit(3)
  .run();

console.log(results.items);

while(results.hasNext()) {
  console.log("---▼ Next Page ▼---");
  results = await results.next();
  console.log(results.items);
}

/* Logs:
 * [{"_id":"San Diego"},{"_id":"Orlando"},{"_id":"San Francisco"}]
 * ---▼ Next Page ▼---
 * [{"_id":"Buffalo"},{"_id":"Miami"},{"_id":"Los Angeles"}]
 * ---▼ Next Page ▼---
 * [{"_id":"Miami"}]
 */