Using MongoDB Aggregation to Build Hierarchical Category Structures
In application development, it’s often necessary to manage and display hierarchical category structures, such as parent categories, subcategories, and sub-subcategories. MongoDB provides a very useful tool for this task through the aggregation framework, specifically with the $graphLookup
stage. Will explain how you can use MongoDB Aggregation Framework to build hierarchical category structures from data stored in a MongoDB collection.
1. Data Structure
First, let’s look at an example of category data stored in a MongoDB collection:
{ "_id" : 1, "cat_id" : 1, "title" : "Parent Category", "parent" : null }
{ "_id" : 2, "cat_id" : 2, "title" : "Child Category", "parent" : 1 }
{ "_id" : 3, "cat_id" : 3, "title" : "Sub Child Category", "parent" : 2 }
In this example:
Parent Category
is the parent category.Child Category
is a subcategory ofParent Category
.Sub Child Category
is a sub-subcategory ofChild Category
.
2. Using $graphLookup
to Build Category Structure
MongoDB provides $graphLookup
in the aggregation pipeline to perform recursive lookups within the collection. This is very useful for building hierarchies like category structures.
Basic syntax for $graphLookup
:
db.categories.aggregate([
{
$match: { "parent": null }
},
{
$graphLookup: {
from: "categories",
startWith: "$_id",
connectFromField: "_id",
connectToField: "parent",
as: "subCategory"
}
}
])
Explanation:
$match: { "parent": null }
: Filters documents to get the parent categories (categories that do not have aparent
).$graphLookup
: Performs a recursive search to find all related subcategories.from
: The name of the collection used for the lookup (in this case, the same collectioncategories
).startWith
: The field from the initial document used as the starting point for the lookup (here,_id
of the parent category).connectFromField
: The field in the initial document used for the lookup (i.e.,_id
).connectToField
: The field in the documents being looked up that will be connected (i.e.,parent
).as
: The name of the new field that will contain the recursive lookup results (in this case,subCategory
).
3. Aggregation Results
The result of this aggregation operation will provide a hierarchical category structure with all related subcategories. Here’s the result from the example data provided:
{
_id: 1,
cat_id: 1,
title: 'Parent Category',
parent: null,
subCategory: [
{ _id: 2, cat_id: 2, title: 'Child Category', parent: 1 },
{ _id: 3, cat_id: 3, title: 'Sub Child Category', parent: 2 }
]
}
In this result, Parent Category
has a subCategory
field containing all related subcategories, including Child Category
and Sub Child Category
.
4. Conclusion
The MongoDB Aggregation Framework with $graphLookup
allows you to efficiently build and display hierarchical category structures. By using this approach, you can easily manage hierarchical data in your application, providing a robust way to organize and display structured information. This technique is valuable in many applications, including e-commerce, content management systems, and more.