RM Hierarchy Management System
Hierarchical RM-client relationship system using Nested Set Model in MongoDB for efficient tree traversal and complex hierarchy queries.
Node.jsMongoDBExpress.jsNested Set Model
RM Hierarchy Management System
A hierarchical relationship management system using the Nested Set Model for efficient tree operations in MongoDB.
Problem
Managing Relationship Manager (RM) hierarchies in a fintech organization is complex:
- Multi-level hierarchies (Regional Head → City Head → RM → Sub-RM)
- Need to quickly find all clients under any RM or their descendants
- Complex reporting across organizational structures
- Traditional parent-child references required recursive queries
Solution: Nested Set Model
Instead of recursive parent-child references, I implemented the Nested Set Model which represents tree structures using left/right boundary values:
How It Works
// Each node has left and right values
// A node's descendants have left > node.left AND right < node.right
{
"name": "Regional Head",
"left": 1,
"right": 14,
"depth": 0
}
// City Head 1: left=2, right=7
// City Head 2: left=8, right=13
Key Operations
- Find all descendants:
left > node.left AND right < node.right— O(1) query! - Find ancestors:
left < node.left AND right > node.right - Count descendants:
(right - left - 1) / 2
Benefits Over Recursive Queries
- 40% faster reads for hierarchy traversal
- Single query for any tree operation
- Efficient reporting across organizational structures
- No recursive functions needed in application code
Architecture
- MongoDB with compound indexes on
[left, right]for fast range queries - Express.js API with CRUD operations that maintain left/right values
- Batch updates for tree modifications to maintain consistency
- Caching layer with Redis for frequently accessed hierarchy data
Results
- 40% performance improvement in hierarchy reads
- Simplified codebase — no recursive query functions
- Scalable to thousands of RM nodes with consistent performance