Recently I write an application that return relation between users.
I have a collection where documents are linked by parent_id :
{
_id:1,
parent_id:0
}
{
_id:2,
parent_id:1
}
{
_id:3,
parent_id:1
}
{
_id:4,
parent_id:2
}
{
_id:5,
parent_id:2
}
{
_id:6,
parent_id:4
}
{
_id:7,
parent_id:4
}
and more...
So how can I return data Like this: I use laravel and mongodb
{
_id:1
children:{
{_id:2,
parent_id:1
children:{
{
_id:4,
parent_id:2
children:{
{
_id:6,
parent_id:4
}
{
_id:7,
parent_id:4
}
}
count:2 //Total number of children to End of This Node
}
{
id:5,
parent_id:2
}
}
count:4 //Total number of children to End of This Node
}
{
_id:3,
parent_id:1
}
},
count:6 //Total number of children to End of This Node
}
How can I get the number of children in each node by aggregate
Thanks everyone:)