vendredi 26 décembre 2014

Mongo building a family tree


I am trying to build a family tree with potentially infinite levels of parents and children. I will also want to find brothers, sisters, cousins, etc. and am a bit confused at the approach of building an array in Javascript when the data only gives parents of every person.


In a MongoDB Collection called "Users" I have the following entries



{ id: 1, name: "Target", parents: [3,4] }
{ id: 2, name: "Wife" }
{ id: 3, name: "Dad", parents: [5,6] }
{ id: 4, name: "Mom" }
{ id: 5, name: "Dads Dad", parents: [7,8] }
{ id: 6, name: "Dads Mom" }
{ id: 7, name: "Dads Dads Dad", parents: 9 }
{ id: 8, name: "Dads Dads Mom" }
{ id: 9, name: "Dads Dads Dads Dad" }
{ id: 10, name: "Son", parents: [1, 2] }
{ id: 11, name: "Sons Son", parents: [10] }
{ id: 12, name: "Sons Sons Son", parents: [11] }
{ id: 13, name: "Brother", parents: [3,4] }
{ id: 14, name: "Brothers Son", parents: [13] }
{ id: 12, name: "Uncle", parents: [5,6] }
{ id: 12, name: "Aunt", parents: [5,6] }


I can easily loop every child using the ids and output it but it will not give structure of children of children just loops and outputs.



getChildren = function(id) {
var children = Users.find({parents: id});
children.forEach(function(child) {
console.log(child);
getChildren(child.id);
});
};


I have been trying to create two global variables ascendants and descendants so that I could loop parents, grandparents, etc. and nest their children and childen's children inside that (for brothers and then nephews, etc). and then similar with descendents. This has turned complex because of having multiple nests and having to be able to add an entry inside another entry.


Was hoping someone could help me with structuring this.





Aucun commentaire:

Enregistrer un commentaire