Class references others of the same class

7 views
Skip to first unread message

Pat Ferrel

unread,
Apr 9, 2012, 7:21:35 PM4/9/12
to mon...@googlegroups.com
I create a mongo db in java and am trying to create the UI in RoR so sorry if this is really stupid question.

The db looks like this:
db.topics.findOne();
{
    "_id" : ObjectId("4f832a433ea0e7fad2121950"),
    "name" : "reuters",
    "clusterID" : "root:reuters",
    "parent" : ObjectId("4f83299f3ea0e7fad212194f"),
    "children" : [
        ObjectId("4f832b1b66775a1ba30c8932"),
        ObjectId("4f8341a566775a1ba30cdd7d"),
        ObjectId("4f8341a766775a1ba30cdd7e"),
        ObjectId("4f8341cb66775a1ba30cdd7f"),
        ObjectId("4f8341cc66775a1ba30cdd80")
    ],
    "docs" : [ ]
})
"parent" is an id of another Topic and "children" is an array of Topic ids.

I know there is a mongoid_acts_as_tree gem to extend mongoid but it does not give me certain other things I need.

Nick Hoffman

unread,
Apr 9, 2012, 9:20:49 PM4/9/12
to mon...@googlegroups.com
Hi Patrick. What's your question?

Pat Ferrel

unread,
Apr 9, 2012, 10:19:07 PM4/9/12
to mon...@googlegroups.com
How does one represent this with mongoid?

Pat Ferrel

unread,
Apr 9, 2012, 10:58:33 PM4/9/12
to mon...@googlegroups.com
I don't even know how to ask this question. I need a class that has a named reference to an object of the same class and a named array of references to the same class.

without persistence it looks like this:

class Topic
     attr_accessor :parent, :children
end

t = Topic.new
p = Topic.new
c1 = Topic.new
c2 = Topic.new
t.parent = p
t.children = Array.new([c1, c2])
t.inspect
=> "#<Topic:0x007f8faa203ac8 @parent=#<Topic:0x007f8faa1fe640>, @children=[#<Topic:0x007f8faa1f6b70>, #<Topic:0x007f8faa1ecd00>]>"

But how does one make Topic persistent with mongoid so objects can be read out of the mongo json structure below? Ideally using the names parent and children (though I suppose I can change them in Java.) I never create the objects in ruby and then do a t.save on them. I do a Topic.find...

Thanks

vierund...@googlemail.com

unread,
Apr 10, 2012, 1:54:43 AM4/10/12
to mon...@googlegroups.com
given the fact that you're only using a tiny fraction of mongoid, wouldn't it be a good idea to drop down to the mongo-driver and implement the two or three methods you need directly on the driver? If you only want a read only/search-ui, you will probably end up with some map/reduce-functions (which also makes it easier to reuse these in java, if need be) for querying and some simple methods for traversing your structure. 

That being said, there is  mongoid_tree (https://github.com/benedikt/mongoid-tree), which gives you a tree-structure and can probably be tweaked to accomodate your java-generated structure (or the other way around, whichever fits better) - even if you do not use it, it gives you a good example of how trees can be modelled&mapped in mongoid.

PS: And I won't even get into the whys of having two separate apps in two different languages for write&read....
Reply all
Reply to author
Forward
0 new messages