0

I'm trying to create a tree structure using associations in Waterline which in the end connects to a MongoDB database. The definition of a treee no is:

TreeNode.js

module.exports = {
    attributes: {
        name: 'string',
        childrenCategory: 'string',
        parent: {
            model: 'TreeNode'
        },
        children: {
            collection: 'TreeNode',
            via: 'parent'
        }
    }
};

And that's it for my tree structure. This node reference itself having multiple childrens of the same node and a unique parent, again of the same node. Populating these tree seems to be working. But trying to find node with a given 'parent' does not. The following query gives nothing:

TreeNode.find().where({parent: someId}).exec(function (err, r) {}

Any ideas? Also, what do you think about this tree structure? Thanks!

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Omplog
  • 11
  • 1
  • The query is not matching to the data structure you have shown. Where is the parent ID stored in your documents? – Sebastian May 06 '14 at 14:05
  • Have a look at [this answer on another post](http://stackoverflow.com/a/22942987/3336235) – aludvigsen May 06 '14 at 14:07
  • Thanks fot the responses. Sebastina, as "parent" is a reference to the same document (TreeNode) the id will be that of the master document. I did not declare "id" because is auto generated, so it should be there. – Omplog May 06 '14 at 14:49
  • The difference in the post suggested, is that in that they use references between 2 diffrent documents and this is a reference to itselft. I don't know if this is possible really, I did not found anything about trees on Waterline. – Omplog May 06 '14 at 14:51
  • This should absolutely work. Can you post some code where you're creating TreeNode instances? Maybe the problem is there. Remember that just adding `children` to an instance won't set the `parent` attribute of those nodes; you'll have to set manually...and vice versa... – sgress454 May 06 '14 at 15:47
  • I have a more detailed explanation on a GitHub thread with Waterline creator. Check https://github.com/balderdashy/waterline/issues/410#issuecomment-42330400 – Omplog May 07 '14 at 15:38

1 Answers1

0

Have a look at this post:

https://github.com/balderdashy/waterline/issues/410

Will it help you if you try using objectid as the type for the parent?

hansmei
  • 660
  • 7
  • 17