Array of associated documents

33 views
Skip to first unread message

myobie

unread,
Jan 11, 2010, 10:21:14 PM1/11/10
to Mongoid
In my project, I have a need for an embedded has_many_related. Yeah, I
know that's crazy.

Simplified, I need Book to be { "authors": [1,2,3] } where 1, 2, and 3
are _id's of Authors.

While I am looking into writing my own module to make this possible, I
was curious if anyone has done this before or if anyone has any ideas
of how best to achieve this?

Durran Jordan

unread,
Jan 12, 2010, 1:23:59 AM1/12/10
to mon...@googlegroups.com
Is authors stored in a relational database? As long as the authors table has a column for book_id that's a String, Mongoid's has_many_related would work out of the box for you... I'm not sure I understand the embedded part of what you're asking. Is author an embedded document in another collection in Mongo?

2010/1/11 myobie <myo...@gmail.com>

Igor Gladkoborodov

unread,
Jan 12, 2010, 3:23:51 PM1/12/10
to Mongoid
In MongoMapper you can store has_many association in array of ids:
many :authors, :in => :author_ids
many :books, :foreign_key => :author_ids

It would be great if we could do it in MongoID's has_many_related.

Eric Falcao

unread,
Jan 12, 2010, 6:58:02 PM1/12/10
to Mongoid
If you take a look at http://www.mongodb.org/display/DOCS/MongoDB+Data+Modeling+and+Rails
they use "voters" as an array of user_ids that have "upmoderated" a
story. I guess the idea is that it could be more efficient than
foreign keys on the other collection (depending on your usage....).

Thank you so much for mongoid. It is very exciting to see such a
fantastic library come along. Versioning is amazing!

-Eric

On Jan 12, 9:23 am, Igor Gladkoborodov <igor.gladkoboro...@gmail.com>
wrote:

Kyle Banker

unread,
Jan 12, 2010, 8:26:31 PM1/12/10
to mon...@googlegroups.com
Eric,

Also, storing voters as an array makes it possible to record an upvote using a single atomic update operation. See this newly added example:


Kyle 

Durran Jordan

unread,
Jan 12, 2010, 11:05:03 PM1/12/10
to mon...@googlegroups.com
Looks like has_many_related gets some love tonight then. :)

2010/1/12 Eric Falcao <efa...@gmail.com>

Thomas R. Koll

unread,
Mar 5, 2010, 8:40:33 AM3/5/10
to Mongoid

On 13 Jan., 00:05, Durran Jordan <dur...@gmail.com> wrote:
> Looks like has_many_related gets some love tonight then. :)

Can you post a follow-up wether anything came out of this night full
with love?

ciao, tom

Reply all
Reply to author
Forward
0 new messages