Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Objects in Tree #3

Open
fatihsoydan opened this issue Nov 5, 2012 · 3 comments
Open

Get Objects in Tree #3

fatihsoydan opened this issue Nov 5, 2012 · 3 comments

Comments

@fatihsoydan
Copy link

Hi;

I want to get objects in a tree like this :

[ {name : 'adam',
childs: [
{
name:'bob',
childs:[
{
name : 'carol',
childs:[]
}
]
},
{
name:'bob2_anotherchild',
childs:[]
}
]
}]

How can do that ?

@fatihsoydan
Copy link
Author

I was write this code to my project, maybe it can be helpful

var ghUser = mdb.model('ghUser');

function findChilds(usr,next){
var childarr = [];
usr.getChildren(function(err,childusers){
var userCount= childusers.length;
if (userCount<=0) {
next([]);
};
childusers.forEach(function(child){
console.log('child:'+child.login);
findChilds(child,function(arr){
userCount-=1;
var obj={};
obj.login=child.login;
obj.childs=arr;
childarr.push(obj);
if (userCount<=0) {
next(childarr);
}
});
});
});
};

ghUser.findOne({login:'fatih.soydan'}, function(e,cuser){
console.log(cuser.login+' '+cuser._id);
findChilds(cuser,function(arr){
console.log("Treejson:"+JSON.stringify(arr));
});
});

Returns that :
[{"login":"huseyin.ozcan","childs":[{"login":"sefa.caksu","childs":[{"login":"zehra.onat","childs":[]},{"login":"deniz.kurt","childs":[]}]}]}]

@franck34
Copy link

I need the same things

I've tried to implement this inside mongoo-tree directly.

Pull request waiting for approval.

@franck34
Copy link

If you want a native implementation, npm install mongoose-tree2

https://github.com/franck34/mongoose-tree

Franck

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants