JavaSheriff
JavaSheriff

Reputation: 7665

Mongodb - hierarchy path query

Given the following collection in mongoDb, I would like to give the user the location in the tree (concatenate the path to the root) as:
Root.Application.Config.Timeouts
Is there an easy way to do this in mongoDb, or I will have to use the find method with the parentHierarchyId recursively until its null?

{
  "hierarchyId" : 0,
  "parentHierarchyId" : null,
  "hierarchyName" : "Root",
}
{
  "hierarchyId" : 1,
  "parentHierarchyId" : 0,
  "hierarchyName" : "Application",
}
{
  "hierarchyId" : 2,
  "parentHierarchyId" : 1,
  "hierarchyName" : "Config",
}
{
  "hierarchyId" : 3,
  "parentHierarchyId" : 2,
  "hierarchyName" : "Timeouts",
}

Upvotes: 0

Views: 780

Answers (1)

yaoxing
yaoxing

Reputation: 4183

This is sort of a classic question. It's not only about MongoDB but also for RDBMS. Answered it once in another form. See if it answers your question:

How should I model my MongoDB collection for nested documents?

Upvotes: 1

Related Questions