We need to show mutual facebook friends for each listings (Similar to what airbnb does). We dont need to query by friends only show mutual connections for each listing. This would be relatively straightforward in a relational database, but I'm somewhat stuck on the best way to accomplish it in MongoDB. What is the best way to query for mutual friends between the logged in user and the listing owner? Should I query for each listing using a foreach loop individually after querying for listings or is there a way to query for both listings and mutual friends in one go?
The current schema that I have is:
Listing: {
user_id: object id,
...
}
User: {
_id: object id,
friends: [{facebook_id: string, name: string}]
...
}
As far as I understand the question, in relational world you would need a simple inner join, right? You do inner join of friends of one user to another, and get mutual friends. This may seem simple, and easy, but under the hood, this is a costly operation and it is not any better than running a custom Map/Reduce in Mongo and get the same data. Internally operation will be the same: simply intersect to lists of ids. Since I'm currently not very good with Map/Reduce, I will not post example. I personally, would just load friends of both users into memory and intersect there. This should not be much worse, then doing inner join in RDBMS.