Re: [mongodb-user] schema for social relationships (friendships)

1,189 views
Skip to first unread message

Sam Millman

unread,
Jul 30, 2012, 3:49:16 AM7/30/12
to mongod...@googlegroups.com
"I'm wondering what approaches people have for dealing with this in Mongo. Should I have embedded docs in each User for 'sent-requests', 'received-requests', and 'friends'?"

Since requests can reach into their thousands and you might need to dynamically show friends and requests based upon conditions I probably wouldn't do this unless you can handle the sorting fully on client side. If you consider the other stuff that would need to be housed in the user soon you will have a friendship limitation (Maybe 2000? Doesn't seem a achievable goal but take into consideration subscriptions and large accounts which should really be pages) which isn't good.

That is why I made the friend relation a separate table like you would do in SQL or other relational DBs.

On 30 July 2012 05:22, Ben McMahen <ben.m...@gmail.com> wrote:
I'm new to the world of non-relational databases, and am struggling a bit with making a proper schema for handling social relationships (like Friendships). In a relational database, like MySQL, I'd do something like this:

from_user : user foreign_key
to_user : user foreign_key
status: 0, 1, 2  // 0 = sent, 1 = accepted, 2 = rejected

I'd then query this table to determine a list of friends associated with a particular user. 

But with a non-relational database, I imagine that this type of schema isn't ideal. Instead, I'm wondering what approaches people have for dealing with this in Mongo. Should I have embedded docs in each User for 'sent-requests', 'received-requests', and 'friends'? The maintenance for this might be high for sending requests, creating friendships, and deleting them, but retrieving the friendships would be fairly easy. 

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Stephen Steneker

unread,
Aug 3, 2012, 3:56:52 AM8/3/12
to mongod...@googlegroups.com
I'm new to the world of non-relational databases, and am struggling a bit with making a proper schema for handling social relationships (like Friendships).

Hi Ben,

The document-oriented data modelling approach has different considerations from the relational model and is more closely aligned to the common use cases in your application.  As you've mentioned, it would be reasonable to optimize for the common use cases like retrieving requests & relationships over the less frequent maintenance activities like deleting relationships.

Here are a few links on schema design that you may find helpful:

Cheers,
Stephen

Reply all
Reply to author
Forward
0 new messages