I'm working on implementing a tree structure (similar to this one in Mongo docs) using Mongoose 3.x, but I'm not sure if you can encapsulate all the logic to load a specific node using its siblings and ancestors in general, specifically how best to work with the functionality of a population where ref is in the same collection as refreer.
In some context, the tree I'm working with is one in which nodes are not edited, but new children can be added at any time to any node. So far, I have worked perfectly with a set of model methods that load objects after the initial find, but it seems like there should be a better way to easily load one branch with all the parent and related data that I need, with one command in the controller and encapsulate all the relevant totality into some convenient search method on the model.
The basic scheme I'm trying to work with might be something like this (also available here: https://gist.github.com/3889616 ):
My hope then would be to be able to load the branch with the corresponding related data through something like the following code, although at the moment I’ve lost quite a lot and could have been quite well abandoned:
req.app.models.Branch .findById(req.param("id")) .populate("parentBranch") .populate("parentBranch._children") .exec(...)
Ultimately, I would like something that I could abstract into the "tree" plugin for Mongoose, but I think I need to get this archiving right first. Any ideas?
FWIW, at the end of the day, I really need data for each branch: parent, next brother, previous brother (both at the time of creation), and all the children of the parent.
Thanks in advance!
Ethan winn
source share