Skip to main content
Skip table of contents

Aggregating Data

Using a MongoDB aggregation pipeline, you can select and aggregate documents. A pipeline is an array of one or multiple stages that will be processed one after the other. Refer to the MongoDB Manual for more information on aggregation and pipelines.

The example below shows a simple aggregation pipeline that consists of one single stage: a $group stage that groups documents and summarizes values

MongoDBAdapter_CustomerData_Example

Click here to download a simple example model that shows the usage of the MongoDB adapter in Scheer PAS Designer.

Creating an Aggregation Pipeline

Aggregation stages can be reflected in the Designer using the following class construct:

The displayed class diagram defines aggregations stages to aggregate property orderValue per country for all or a selected country.

Class

Description

(1)

Stage $group

Describes a group stage.

  • As a class property cannot have a name $group,  you need to apply stereotype XMLElement and external name $group.

  • Attribute _id is fix and contains the name of the property to group by. In this example the property to group by is fix. It is $address.country which you need to set before creating the pipeline (see Building the Aggregation Pipeline below).

  • The attributes need to be in exact that order to build a correct group stage, therefore they have stereotype XMLElement and order applied.

(2)

Sum Operator

  • The structure below the $group key defines the $sum part of the grouping.

  • The sum operator (sumOperator with external name $sum) contains the name of the document property to summarize. In this example the property to summarize is fix. It is $orderValue which you need to set before creating the pipeline (see Building the Aggregation Pipeline below).

You can add other stages (e.g. a $match stage) to this structure using the same pattern.

Aggregating Data

Building the Aggregation Pipeline

The action script below shows how to build the pipeline.

Action Script

Explanation

CODE
buildGroupString(out pipelineStructure: AggregateOderValue,
                 out grouping: AggregationGroup,
                 out sum: AggregationSum,
                 out pipeline: String[])

Parameters of the action script operation.

CODE
create pipeline;
create pipelineStructure;
create grouping;
create sum;
  • Create the pipeline array.

  • Create an object of the pipeline structure you have defined before. In this example, this is piplineStructure : AggregateOrderValue.

  • Create the $group stage and it's contained $sum operator.

CODE
set pipelineStructure.groupOperator = grouping;
set pipelineStructure.groupOperator._id = "$address.country";
  • Assign the group stage to the pipeline structure.

  • Assign the name of the document property to group by to _id.

CODE
set pipelineStructure.groupOperator.sumOrderValue = sum;
set pipelineStructure.groupOperator.sumOrderValue.sumOperator = "$orderValue";
  • Assign the sum operator to the group stage.

  • Assign the name of the document property to summarize to sumOperator.

CODE
append pipelineStructure.classToExtendedJSON() to pipeline;

The resulting aggregation pipeline will look like

CODE
{ "$group" : { "_id" : "$address.country", "sumOrderValue" : { "$sum" : "$orderValue" } } }

Aggregation Result

As a result of the aggregation, you will get a JSON document that contains the following order value aggregation:

CODE
{       
    "orderVolume": [
        {
            "_id": "USA",
            "sumOrderVolume": 1098.0
        },
        {
            "_id": "CA",
            "sumOrderVolume": 180.0
        }
    ]
}

If you provide an array of a result structure as an output for the adapter call, the xUML Runtime will map the results accordingly:

Related Documentation:

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.