Manisha Shire

MULTIDIMENSIONAL GROUPING in MongoDB Using $facet Aggregation

By Manisha Shire

Last updated cal_iconFebruary 24, 2023

According to MongoDB official documentation, they describe ‘$facet’ as:

“Processes multiple aggregation pipelines within a single stage on the same set of input documents. Each sub-pipeline has its own field in the output document where its results are stored as an array of documents. 

The $facet stage allows you to create multi-faceted aggregations which characterize data across multiple dimensions, or facets, within a single aggregation stage. Multi-faceted aggregations provide multiple filters and categorizations to guide data browsing and analysis. Retailers commonly use faceting to narrow search results by creating filters on product price, manufacturer, size, etc.”

So, first of all, Why do we use ‘$facet’, What is ‘$facet’ mainly useful for or and what is ‘$facet’?

MongoDB

Sometimes when we are creating a report on specific data, you get to know that we need to do the same preliminary processing for a number of reports, and you have to create and maintain an intermediate collection though out the processing. For better optimization of the mongo query, we use “$facet” to improve the performance of the mongo Query. 

For a better understanding of “$facet” you can think of it as follows:

In the restaurant, you have ordered 2 different plates of Pasta, so what the chef will do is, he will keep the same pasta and then put other ingredients like sauces and veggies in it accordingly.

Examples

So that was basically a short introduction of what $facet exactly is.

Now, will take a look at the problem scenario, which I recently faced and how I overcame it with the $facet.

Let’s suppose there is a collection in the MongoDB document named metricsCaptured, which holds basic details like name, region, and count.

I wanted the data of the top 5 records of the AMER region and the top 5 of the APAC region. ( I was using Node.js with MongoDB )

I first chose the fields from the main collection named “metricsCaptured” and used $unwind on that array field. then I used  $facet for all those necessary fields as an individual array like AMER_region and APAC_region. In that used $match to get all records of AMER and APAC regions and then used $sort, $limt, and $project to get the top 5 records of metricscaptured from the main collection.

metricscaptured

In the processing documents, each sub-pipeline within $facet like APAC _region and AMER_region is given the exact same set of input documents from stage $match. These sub-pipelines are completely independent of each other and the array output is stored in separate fields in the output document i.e APAC_region and AMER_region.

This process is way more efficient and in case of any future change depending on the requirement, all you need to do is add or remove more sub-arrays from the facet without disturbing the other arrays.

Conclusion:

“$facet” is nothing but multiple aggregation pipelines within a single stage on the same set of input documents.

Get In Touch

How Can We Help ?

We make your product happen. Our dynamic, robust and scalable solutions help you drive value at the greatest speed in the market

We specialize in full-stack software & web app development with a key focus on JavaScript, Kubernetes and Microservices
Your path to drive 360° value starts from here
Enhance your market & geographic reach by partnering with NodeXperts