Run an Aggregation Pipeline

The aggregation pipeline lets you perform complex data transformations and calculations on collections. This guide explains how to build and run an aggregation pipeline. A pipeline consists of 1 or more stages that process your data sequentially, with each stage's output becoming the input for the next stage. The pipeline allows you to:

  • Group items and calculate aggregate values.
  • Transform result items by including, excluding, or reshaping fields.
  • Filter, sort, and limit results.

Note: You can only build an aggregation pipeline for collections created in the CMS or with the Data Collections API. You can't use them on Wix app collections or external collections.

Anatomy of a pipeline

  • aggregatePipeline(): Runs an aggregation pipeline on a specified collection and returns the results.
  • pipelineBuilder(): Builds an aggregation pipeline with stages and pagination configuration.
  • Stages: Processing operations that transform data sequentially. Each stage type performs a different operation.
  • Expressions: Operators and values for performing calculations, transformations, and field manipulations in some stages.

Build and run an aggregation pipeline

To run an aggregation pipeline, first build it by calling pipelineBuilder(). After you build it, specify the pipeline to the aggregatePipeline() method to retrieve the aggregated results:

  1. Import the items module and deconstruct the required submodules:

    Copy
  2. Add 1 or more stages to build the pipeline:

    Copy

    Some stages use expressions to resolve the values required for the specified operation:

    Copy
  3. Specify the stages array to the withStages() method. You can also add paging configuration with the withPaging() method. Finally, call build() to build the pipeline:

    Copy

    Your pipeline is now ready.

  4. Specify the configured pipeline to the aggregatePipeline() method to run the pipeline:

    Copy

See also

Did this help?