Using as a graph database for finding "friends" of "friends" in MongoDb
Although it wouldn't be impossible, MongoDB would not be a good fit for this scenario.
The reason is that MongoDB does not do JOINs. When you need a query which spans multiple documents, you need a separate query for each document.
In your example, each user
document would have an array with the _id
's of their friends. To find "all friends of the friends of UserA who are also friends of UserB" would mean that you would:
- find userA and get his friends-array
- find all users in that array and get their friend-arrays
- find all users in these arrays who have UserB in their friends-array
These are three queries you have to perform. Between each of these queries, the result set has to be sent to the application, the application has to formulate a new query and send it back to the database. The result-set returned from the 2nd query can be quite large, which means that the 3rd query could take a while.
tl;dr: Use the right tool for the job. When your data is graph-based and you want to do graph-based queries on it, use a graph database.