MongoDB’s BI Connector is a powerful tool that enables users to perform SQL queries on MongoDB data using traditional BI (Business Intelligence) tools such as Tableau, Qlik, and Microsoft Excel. However, the structure of MongoDB documents does not always fit well with the tabular structure that BI tools expect.
This is where the aggregation pipeline comes in - it allows users to shape and format the data in MongoDB to meet the needs of the BI tool. The aggregation pipeline is a framework for performing complex data transformation and analysis operations on MongoDB documents. It includes a set of stages that can be used to filter, group, sort, and aggregate data in a flexible and powerful way.
For example, let’s say we have a collection of customers with the following structure:
{
_id: ObjectId("5fca48d94108d6a3a66e464a"),
name: "John Smith",
age: 35,
gender: "male",
address: {
street: "123 Main St",
city: "Anytown",
state: "CA",
zip: "12345"
},
purchases: [
{ product: "Shoes", price: 50 },
{ product: "Shirt", price: 25 },
{ product: "Pants", price: 40 }
]
}
To use this data in a BI tool, we may want to aggregate the total price of all purchases for each customer, as well as the total revenue for the entire collection. We can accomplish this with the following aggregation pipeline stages:
[
{
$unwind: "$purchases"
},
{
$group: {
_id: "$_id",
name: { $first: "$name" },
total: { $sum: "$purchases.price" }
}
},
{
$group: {
_id: null,
totalRevenue: { $sum: "$total" },
customers: { $push: "$$ROOT" }
}
}
]
Let’s break down what’s happening in each stage:
1. ‘$unwind‘: This stage "flattens" the ‘purchases‘ array, creating a separate document for each purchase. Our example document would now become three separate documents, each with a different ‘purchases‘ object.
2. ‘$group‘: This stage groups the documents by ‘_id‘ (which is the same for each document since we haven’t done anything to change it). We also use the ‘$first‘ operator to ensure that the ‘name‘ field is available to us in the next stage. Finally, we use the ‘$sum‘ operator to add up the ‘price‘ field for each purchase within the group.
3. ‘$group‘: This stage groups all of the documents from the previous stage together into a single document with ‘_id‘ set to ‘null‘. We use the ‘$sum‘ operator again to calculate the total revenue for the entire collection. We also use the ‘$push‘ operator to create an array of the customers, each with their own ‘total‘ field calculated in the previous stage.
The result of this aggregation pipeline would be a single document with the following structure:
{
_id: null,
totalRevenue: 115,
customers: [
{ _id: ObjectId("5fca48d94108d6a3a66e464a"), name: "John Smith", total: 115 }
]
}
This document can now be queried by a BI tool using standard SQL syntax. We can connect Tableau to MongoDB using the BI Connector and write a query like this:
SELECT name, total FROM mydb.customers
The result would be a simple table with the name and total for each customer, ready to be visualized in Tableau or any other BI tool.
In summary, the aggregation pipeline plays a critical role in shaping MongoDB data for use in BI tools. Its flexible, modular structure allows for complex data transformation and analysis operations that can be tailored to meet the specific needs of the user.