WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

MongoDB · Advanced · question 60 of 100

Explain the role of the aggregation pipeline in MongoDB’s BI (Business Intelligence) Connector.?

📕 Buy this interview preparation book: 100 MongoDB questions & answers — PDF + EPUB for $5

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.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic MongoDB interview — then scores it.
📞 Practice MongoDB — free 15 min
📕 Buy this interview preparation book: 100 MongoDB questions & answers — PDF + EPUB for $5

All 100 MongoDB questions · All topics