Tuesday, March 11, 2014


Fun with mongodb and the Facebook Graph API

In a previous post we showed how to make http calls from within the mongodb shell. Another nice example of what we can do with this wget function is to make calls to the facebook graph API. Since the fb graph API uses json, we can store the results immediately in the database.

We put all the relevant functions and parameters in a single object (and even save that in the db. I prefer this to using .mongorc or the system.js collection. I'll write another blog post about this technique later).

We create a facebook object and copy the wget function in it.
var facebook = {
        "wget" : function wget(url){
        var tmp = "/tmp";
        var id = new ObjectId();
        var outFile= tmp+"/wget"+id;
        var p = run("wget","-o log","--output-document="+outFile,url);
        if (p==0){
        var result = cat(outFile);
        return result;
        } else {
        return "";

(Allmost) all calls to the API need an access token. You can generate a token (with the access rights you need) at https://developeallows us tors.facebook.com/tools/explorer which is valid for 2 hours.

facebook.token =  "CAACEdEose0cBAFzPP7M........udv2TjMTQQKEZD";
Now we create the function to make the graph call itself.
facebook.graph  = function graph(user,item){
        if (!user){
           user = "me";
        var url = "https://graph.facebook.com/"+user;
        if (item){
           url = url+"/"+item;
        var result =  this.wget(url);
        return JSON.parse(result);

This function graph makes a call to the graph API (using the wget function) with 2 (second optional) parameters.
  • user (which defaults to 'me')
  • item: specifies what object from the graph you want  (friends, events, photos, ...)
Let's try a few things:

facebook.graph('me','friends');  shows a list of all my friends.
facebook.graph('mongodb'); shows info about the mongodb page

When returning a single object, the json reply simply describes the object. When the API returns a list of objects, this list is an array named 'data' (mostly followed by pagination url's).
The nice thing is that the function returns a JSON object, so it's very easy to save the results in the database.
var friends = acebook.graph('me','friends');
friends = friends.data;
 Or simply print a list of your friends
Now we can loop over all these friends and lookup basic info for each of them with this code
friend.info = facebook.graph(friend.id); db.friends.save(friend);})

There's lots of extra functionality that we could add (extra parameters like 'limit', use different access_tokens for different users, use POST and DELETE calls instead of only GET ...)

Have fun!!


  1. Hi Van ,

    this is a great tutorial .. could you please make a tutorial for importing group feed using graph API and storing it in to mongo DB

    Also querying the json from Mongo DB ? I want to make search engine for my facebook group and I would like to store the feed in to mongo DB rather than MYsql which is the standard way ?

    could you please help me out with the tutorial to store/retrieve json group feed to/from mogno DB

    Thanks in advance

  2. Hi, Shashi. I'm trying to do the same. Did you find a way to import group feed into a DB?

  3. hi sir how are u!
    how to store facebook data through graph api in mangodb ?