How to Perform Aggregation in MongoDB Compass Community

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

MongoDB Compass Community provides users a quick and easy way to execute a MongoDB document aggregation. Aggregation in MongoDB Compass Community is a simple and easy, but sophisticated, program that provides users with multiple MongoDB Compass options, including how to analyze documents and visualize and explore schemas to understand the types, ranges and frequency of fields in a data set. The Aggregation Pipeline Builder in MongoDB Compass Community offers the user the ability to create aggregation pipelines to process data so documents in a collection can pass through multiple stages where the documents are processed into a set of aggregated results, using the aggregate method in MongoDB Compass.

Prerequisite

  • Confirm that the MongoDB service is running.

  • Confirm there is a working version of the MongoDB Compass application installed before proceeding.

NOTE: This tutorial uses the MongoDB Compass Community Edition version 1.18.0

The MongoDB Dataset

The following dataset will be used throughout this tutorial:

1
2
3
4
5
6
{ "_id" : ObjectId("5cee629f3fd48a38b2d163bd"), "name" : "Chang Kai-shek", "country" : "China", "rating" : 11 }
{ "_id" : ObjectId("5cee629f3fd48a38b2d163c1"), "name" : "Cao Cao", "country" : "China", "rating" : 19 }
{ "_id" : ObjectId("5cee629f3fd48a38b2d163c2"), "name" : "Sun Tzu", "country" : "China", "rating" : 10 }
{ "_id" : ObjectId("5cee629f3fd48a38b2d163bf"), "name" : "Muammar al-Gaddafi", "country" : "Libya", "rating" : 15 }
{ "_id" : ObjectId("5cee629f3fd48a38b2d163c0"), "name" : "Oda Nobunaga", "country" : "Japan", "rating" : 15 }
{ "_id" : ObjectId("5cee629f3fd48a38b2d163be"), "name" : "Sun Yat-sen", "country" : "China", "rating" : 8 }

Starting MongoDB Compass Community

Begin by opening MongoDB Compass and connecting to the database. The service will prompt the user for the host and a port number. This tutorial will work on localhost and will run MongoDB on the default port 27017. This will result in the program accepting all of the defaults so the user simply has to hit “Connect”.

NOTE: For more information on how to set-up databases in MongoDB Compass Community, refer to: How to Setup a Database in Compass Community

Once MongoDB Compass is connected to the MongoDB service it will prompt a window showing all databases in that specific connection.

How to Use the MongoDB Compass Community Aggregation Pipeline Builder

The MongoDB Compass Community tool is a useful feature that provides an easy way of developing a complex aggregation of pipeline queries.

For this demonstration, first select warlordDatabase, then warlordCollection and then click the “Aggregations” tab from the menu. Here is the resulting image:

MongoDB Compass Community Aggregation Pipeline Builder, with selected database and collection

To start the first stage of the aggregation, first click on “Select” from the drop-down list and then select $match.

Compass will then ask the user to type in the desired expression. Below are the possible actions:

ValueCompass intellisense
[]Compass requires array expression
Compass will list all fields associated with selected collection
$Compass will show in a form of drop-down list all operators allowed within that particular operation

The following example will execute a match for all instantiates of “warlord(s)” from the countries containing the alpha characters “Chi”:

1
2
3
{
"country":/Chi/
}

MongoDB Compass Community Aggregation Pipeline Builder, using $match operator to filter document results

Once the operation is completed on a particular stage the result will then be displayed on the right-hand side of the image. The below example shows that several documents were found:

MongoDB Compass Community Aggregation Pipeline Builder, $match operation returns 4 MongoDB documents

Notice the information compass provided in the result pane stating “Output after $match stage(Sample of 4 documents)”.

To add another stage simply click the “Add Stage” button and then execute the same procedure with other operators, such as $project, $unwind, $lookup, etc.

Once done withthe desired operation(s), the queries can be saved, retrieved and exported by clicking the “Save Pipeline” button, as shown here:

MongoDB Compass Community Aggregation Pipeline Builder, saving the aggregation queries

Conclusion

This tutorial demonstrated how aggregation in MongoDB Compass Community can provide users with multiple MongoDB Compass options, offering quick and easy ways to execute a MongoDB document aggregation and perform complex queries with the use of the Aggregation Pipeline Builder framework. When using the Aggregate Method in MongoDB Compass, users should bear in mind that the examples in this tutorial were executed with version 1.18.0 of MongoDB Compass Community. Results may differ from those in this tutorial if using other versions.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.