Q61 [Scenario]: How do you store and query hierarchical category tree structures (e.g., e-commerce categories) in MongoDB?

Common pattern models:

  • Parent References: Each node stores { _id, name, parent: ObjectId } (fast updates).
  • Materialized Paths: Each node stores { _id, name, path: ',Electronics,Computers,Laptops,' } (fast sub-tree regex queries matching ^,Electronics,Computers,).
  • Array of Ancestors: Each node stores { ancestors: [ObjectId1, ObjectId2] }.
Key Takeaways
  • Materialized Paths enable fast sub-tree searches via regex index prefixing.
  • Array of Ancestors facilitates fast breadcrumb rendering.
Knowledge Check

Which hierarchy modeling pattern stores ancestral paths as string paths for fast sub-tree regex searches?