Reputation: 2012
I'm currently struggling with a task, which could be easily solved in a relational DB with a simple JOIN.
I have a collection user_relations
which stores relations between users in the system and essentially stores referrences to documents from collection users
as a simple _id
. Now, we need user to be able to search among users who are related to him/her. How do you accomplish this in mongo (with a performance in mind, considering the fact that user can have a really large number of related users - friends/followers/etc)?
Upvotes: 0
Views: 53
Reputation: 51
You cannot join collections together in mongodb itself.
Instead you either need to structure your DB design so that say in the example, user_relations
would be an attribute of users
.
Or- perform the join effectively in the client app once you have the keys / _ids of the users you want to fetch.
Because you can't perform these joins on the server, it really forces you to consider exactly what information you want to return.
You could easily pass an array of user _ids you want to return and use the $in
operator to return a set of user documents.
Hope that helps.
Upvotes: 2