2

I am new to MongoDB. I have designed a app with a follower/following like Twitter style

My schema is as follows

UserSchema :

    username:'Alex',
    pass:'salted',
    likes:'200'

FollowSchema

    uid : {
        type: Schema.ObjectId,
        ref: 'User'
    },
    fid : {
        type: Schema.ObjectId,
        ref: 'User'
    }

First of all, is this a scalable design ?

If so, I would like to get all the users except logged in user and find if each user is following the logged in user or not.

I am thinking of Aggregation or mapreduce but not sure how to approach.Any help is appreciated.

Thx

2 Answers2

2

Yes this looks like a good way to do it, according to this post here:

mongo db design of following and feeds, where should I embed?

as well as the mongodb website:

http://blog.mongodb.org/post/61499097398/tracking-twitter-followers-with-mongodb

Community
  • 1
  • 1
1

In mongodb there is no join operation. In mongoose you can use populate().

http://mongoosejs.com/docs/populate.html

Follow.find({}).populate('uid').populate('fid').exec(function (e, d) {
   if(e){
      res.json(e);
   }
   else {
      res.json(d);
   }
});
Libu Mathew
  • 2,976
  • 23
  • 30
  • Thanks...But how do I get all the users first and then find if a specific user is following the logged in users.As per your code, unless the user is in the follow document,I would not get the user details in results. – Karthik Renkarajan May 04 '16 at 19:55