Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store/Sync Facebook Graph in a NoSQL (MongoDB)

I'm building an app that needs to be able to extend facebook graph data.

I'm new to NoSQL storage and i'm looking for help.

Using the graph api, i can retreive a user, since i would like my app to be able to extend several social graph providers, i move every specific facebook keys retreived into a facebook array subset.

[User] => Array
    (
        [_id] => 4dd50c139bcb233c0c000000
        [name] => Foo Bar
        [first_name] => Foo 
        [last_name] => Bar
        [username] => fbar
        [location] => Array
            (
                [id] => 110774245616525
                [name] => Paris, France
            )

        [gender] => male
        [email] => [email protected]
        [timezone] => 2
        [locale] => fr_FR
        [facebook] => Array
            (
                [id] => 12345678
                [link] => http://www.facebook.com/foobar
                [verified] => 1
                [updated_time] => 2011-05-16T17:30:23+0000
                [picture] => https://graph.facebook.com/12345678/picture
            )

        [created] => MongoDate Object
            (
                [sec] => 1305807891
                [usec] => 0
            )
    )
  • Is this good practice ?

Then i grab his friends, and i want to be able to keep them in sync with my database. I don't know if i should register each friend as separate users and try to use references, or if i can just add a Friend subset.

  • What would have the best performance and the easiest to keep in sync ?

       [User] => Array
       (         
            [Friend] => Array
            (
                [0] => Array
                    (
                        [id] => 12345678
                        [name] => Foo Bar
                    )
    
                [1] => Array
                    (
                        [id] => 12345678
                        [name] => Foo Bar
                    )
    
                [2] => Array
                    (
                        [id] => 12345678
                        [name] => Foo Bar
                    )
    
  • Problem rise to another level with FriendLists, how should i store them ?? embed everything (and have a ton of duplicates in my User) or use reference ? How should i do that ?

I read : http://www.mongodb.org/display/DOCS/Trees+in+MongoDB which is quite helpfull... But i'm still unsure what i should do.

Thanks a lot.

like image 332
Olivier Avatar asked May 19 '11 12:05

Olivier


People also ask

Does MongoDB have graph database?

MongoDB as a Graph Database. MongoDB offers graphing capabilities with its $graphLookup stage. Give $graphLookup a try by creating a free cluster in MongoDB Atlas. Graph databases fulfill a need that traditional databases have left unmet: They prioritize relationships between entities.

Is MongoDB a NoSQL database?

NoSQL databases come in a variety of types including document databases, key-values databases, wide-column stores, and graph databases. MongoDB is the world's most popular NoSQL database.

Does Facebook use a graph database?

Every time you visit LinkedIn and see first-, second-, or third-degree connections, you're getting results from the social networking site's professional network graph built on a graph database. Facebook, Instagram and Twitter all use graph databases and analytics to understand how users relate to each other and ...


2 Answers

I had a similar application and I stored friends as an other user. But be sure that you have a flag that indicates if that user is an application user or not, or the data would be somehow chaotic at some point.

for user:

{
  fbid: xxxx,
  name : "xxxxx",
  ......
  friends : [ xxxx, xxxx, xxxx ],
  is_app_user : true
}

for each friend (who are not application user) :

{
  fbid: xxxx,
  name : "xxxxx",
  is_app_user : false
}

and when they also login you can make is_app_user : true for them aswell.

PS: dont forget to put unique index on fbid

like image 118
frail Avatar answered Nov 15 '22 04:11

frail


This is a question of "embed vs. reference".

Here's one good reply on SO.

Here are the official docs on schema design. They're a good place to start looking at these questions.

like image 44
Gates VP Avatar answered Nov 15 '22 04:11

Gates VP