Skip to main content
Skip table of contents

Aggregating Data

This page explains the MongoDB Adapter in Bridge context. If you were looking for the same information regarding the PAS Designer, refer to MongoDB Adapter in the Designer guide.

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 two stages:

  • a $match stage that selects documents from the database
  • a $group stage that groups documents and summarizes a value

Example File (Builder project Add-ons/MongoDB):

<your example path>\Add-ons\MongoDB\uml\simpleMongoDbAccess.xml

Creating an Aggregation Pipeline

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

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

ClassDescription
1

Stage $match

Describes a match stage.

  • As a class property cannot have a name $match,   you need to apply stereotype <<E2EAttribute>> and external names $match.
  • The attribute of type AggregationMatch gives the name and value of the document property to select by. In this case, this is address.country, (attribute country with the corresponding external name) and its value.
2

Stage $group

Describes a group stage.

  • As a class property cannot have a name $group,   you need to apply stereotype <<E2EAttribute>> and external names $group.
  • Attribute _id is fix and contains the name of the property to group by. In this case, this is $address.country which is set as a default value to the _id attribute.
3

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, so it is given as a default value ($orderVolume).

You can add other stages to this diagram using the same pattern.

Aggregating Data

The activity diagram below shows how to build the pipeline and trigger the aggregation.

StepDescription
1

Build the Match Stage

  • Create an object of the pipeline structure you have defined before. In this example, this is piplineStructure : AggregateOrderVolume.
  • Create the $match stage.
  • Set the value to compare with to the match expression.
  • Append the stage to the pipeline array.
2

Build the Group Stage

  • Create an object of the pipeline structure you have defined before. In this example, this is piplineStructure : AggregateOrderVolume.
  • Create the $group stage and it's contained $sum operator.
  • Append the stage to the pipeline array.

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

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

Related Documentation:

JavaScript errors detected

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

If this problem persists, please contact our support.