I'm developing an application that will need to know the friend's connection between registered users in my system.
So, importing those friends are one step of the process. However, I want to do it in the best way possible using MongoDB to reduce CPU and Query time.
I have basically 2 questions:
1) Should I keep the friendList record of each user inside it's own model (User), or with reference, in a new model (FacebookFriendship)?
// Nested (User Model)
{
[...],
facebookId: 12345,
facebookFriends: [123456, 12313, 1241],
}
// Reference (FacebookFriendship Model)
{
friendA: 12345,
friendB: 12344,
// Or maybe an array?
friendship: [12345, 12344]
}
2) Other than the Model structure (That gives me a headache), I'm trying to figure out the proper way of importing things to the server.
Since I only need to know the friendship of those users that are already registered, I don't want to import all the friends of that facebook User.
In resume, what I want is to import those friendships, only if A and B side are both registered (The new user, and it's friend).
What I thought is something like this:
foreach Friend of newUser as friend:
if FacebookUserExistsInServer(friend):
createFriendShip(friend, newUser);
However, this approach requires a LOT of queries. Let's say 500 friends: 500 * 2 = 1000 queries for just one 'new' user. And I would still need to 'refresh' that data latter.
Is my approach a little too intensive, or ok for a normal application?
Thanks anyway!