Click here to Skip to main content
14,690,956 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

we have a required to show data in tree format.


Sample Data:


db.FamilyTree.insert({"id":"100", "ParentName":"Jhon Smith", "Children" : "Michael Smith" })
db.FamilyTree.insert({"id":"100", "Diana Smith":"Jhon Smith", "Children" : "Diana Smith"})
db.FamilyTree.insert({"id":"101", "ParentName":"Michael Smith", "Children" : ""})
db.FamilyTree.insert({"id":"102", "ParentName":"Diana Smith", "Children" : "Britney Smith"})
db.FamilyTree.insert({"id":"301", "ParentName":"Britney Smith", "Children" : ""})
db.FamilyTree.insert({"id":"200", "ParentName":"Richard Smith", "Children" : "M Smith" })
db.FamilyTree.insert({"id":"200", "ParentName":"Richard Smith", "Children" : "D Smith" })
db.FamilyTree.insert({"id":"201", "ParentName":"M Smith", "Children" : "" })
db.FamilyTree.insert({"id":"202", "ParentName":"D Smith", "Children" : "" })



the output of about data should be like below:

   [
   {
      "id":"100",
      "name":"Jhon Smith",
      "children":[
         {
            "id":"101",
            "name":"Michael Smith",
            "children":null
         },
         {
            "id":"102",
            "name":"Diana Smith",
            "children":[
               {
                  "id":"301",
                  "name":"Britney Smith",
                  "children":null
               }
            ]
         }
      ]
   },
   {
      "id":"200",
      "name":"Richard Smith",
      "children":[
         {
            "id":101,
            "name":"Michael Smith",
            "children":null
         },
         {
            "id":"102",
            "name":"Diana Smith",
            "children":null
         }
      ]
   }
]


What I have tried:

I Have tried with $graphLookup but I didn't get required output:


<pre>db.FamilyTree.aggregate( [
        { $project: { _id: 0}},
   { 
      $graphLookup: {
         from: "FamilyTree",
         startWith: "$Children",
         connectFromField: "Children",
         connectToField: "ParentName",
         as: "TreeSearch"

     
      }
   }
] )



Please share if you already faced this kind of scenario.
Posted
Updated 18-Aug-20 8:55am
Comments
Gerry Schmitz 18-Aug-20 11:57am
   
db.FamilyTree.insert({"id":"100", "Diana Smith":"Jhon Smith", "Children" : "Diana Smith"})

Doesn't follow the "pattern".

1 solution

As long as you have the data with Parent Child relation maintained you can show it. From example, seems you have that data in place.

Following references will help guide on how to do it:
Storing Tree Structures in MongoDB: Code Examples[^]

Based on: Model Tree Structures with an Array of Ancestors — MongoDB Manual[^]

Now, with graphlookup you tried, a similar effort few years back by someone: MongoDB's $graphLookup trying to get a tree structure - Stack Overflow[^].
MongoDB Tree Model: Get all ancestors, Get all descendants - Stack Overflow[^]
Quote:
$graphLookup is not producing the hierarchy of dependencies - it performs a recursive search of connected documents, but results are flattened into the single-dimension array

This will not give you the structure you are looking for. Better to store it as Tree model structure shared above to retrieve it back.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900