XCEPTION
XCEPTION

Reputation: 1753

Mongodb Storing Friends Relationship

I am using MongoDb for one of the mobile app that we are developing. It has a feature of sync contacts.

I wanted to know the ideal way of storing the relationships(friends relationship and not RDBMS kind of relationship) in mongodb. I want to know the architecture for the same.

I have thought of the following user collection structure:

{
    _id: ObjectID(abc),
    name: "abc",
    contacts: ["def", "ghi"]
}

In the above collection I am considering "def" and "ghi" as object ids of friends of user abc. Is this the correct way of doing it or can someone suggest me a better and right way that they might have implemented?

All I am concerned about is I should not get stuck or hit the performance when retrieving data specific the user's friends in future.

Consider If I want to get all the activities from collection Activities done by my friends.

Upvotes: 1

Views: 1232

Answers (1)

profesor79
profesor79

Reputation: 9473

I think you could use advantage of noSql structure and save/serve some more info about friend

{
    _id: ObjectID(abc),
    name: "abc",
    contacts: [{id:"def" name:"John"}, {id:"ghi", name:"Sari"} ]
}

To display basic list you will need just one get query, and then having name (or other important related details) - check for activities.

The extra overhead with this structure is a need to update name (and other details) every time when user updates it's name - but this is not a hammer - who changes its name frequently?

Upvotes: 3

Related Questions