Beginner question regarding joins

326 views
Skip to first unread message

Jonas Wagner

unread,
Jun 15, 2010, 1:37:50 PM6/15/10
to mongodb-user
Hi Guys,

I have got a few questions about mongodb.
I'm currently developing a little web
application to learn more about mongodb.
The basic idea is that users can define exercises
that they want to practice and then schedule
them in given intervals. At the base
there are 3 entities. They look roughly
like that:

- User
- name
- email
- password
- Exercise
- owner
- title
- description
- Schedule
- owner
- exercises [{interval: _, exercise: _}]

I'm currently mapping those 1:1 to mongodb collections.
Things start to get a bit hairy. I want the user to be
able to browse a list of all exercises. In that list
I want to show the title of the Exercise and the name of
the owner.

Now the question is how do I do that efficiently?

Using MapReduce seems to be overkill and relatively slow.
I don't really want to have javascript in my python code
for such a simple query.

Putting the name of the user into the exercise (and every
other entity) will cause trouble when the username changes
or when I want to display more information about the user.

Looking up every user individually is going to be slow.
Writing some code that gets all the users at once feels
like reimplementing joins myself. Cacheing users could
be an option, but feels like the wrong solution too.

What am I missing? What is the right way to solve this problem?
Is my database layout wrong?

Thanks for your help,
Jonas

Tony Hannan

unread,
Jun 15, 2010, 5:17:49 PM6/15/10
to mongod...@googlegroups.com
Hi Jonas,

If the exercises are unique to each user, then I would embed them inside the user object. However, if they are general exercises and you want all users to see all exercises, then I would embed desired user fields inside each exercise document. Like you said this is not ideal, a join would be more logical, but a join is more work especially in a distributed (sharded) environment. Because MongoDB's goal is scalability and performance, it has forgone joins. 

Cheers,
Tony


--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


harryh

unread,
Jun 15, 2010, 5:29:44 PM6/15/10
to mongodb-user
> Writing some code that gets all the users at once feels
> like reimplementing joins myself.

This is what you should do. Grab a page worth of exercises, round up
the owner ids, then query for the relavent users.

With MongoDB (or any datastore designed to work at a large scale)
joins are done in the application code.

-harryh

Jonas Wagner

unread,
Jun 16, 2010, 10:28:01 AM6/16/10
to mongodb-user
Hi Guys,

Thanks for the replies.

Embedding the username could be an option. But for values that change
more
frequently that's not an option. So I guess I'll have to do the joins
in some
abstraction/mapper. Surprisingly enough, none of the python mappers
seems
to support this out of the box.

Thanks,
Jonas
Reply all
Reply to author
Forward
0 new messages