Follow action schema for Mongo DB

Schema design for twitter like follow action with MongoDB

It has been asked so many times on stack overflow and other tech blogs. And there are many effectively solutions are already there. Just consider it YASS “Yet Another Schema Solution”.

  • All the schemas are a separate collection, you can name it “follow” or “what the fcuk” if you like it that way.
  • The absence of end field indicates that the user is still following and vice versa. Keeping it this way, helps you track the history without much complications.
If your followers and followee are only one data node e.g. users, this schema will work for you.
{
follower : ‘id’,
followee : ‘id’,
start : ‘date’,
end : ‘date’
}
If your followers are only users but your followees can be multiple data nodes e.g. page, user etc, this schema will work for you.
{
follower : ‘id’,
followee : ‘id’,
followee_collection : ‘collection name’,
start : ‘date’,
end : ‘date’
}
If your followers and followees can be multiple data nodes e.g. page, user etc, this schema will work for you.
{
follower : ‘id’,
follower_collection : ‘collection name’,
followee : ‘id’,
followee_collection : ‘collection name’,
start : ‘date’,
end : ‘date’
}

Well, that’s it. Enjoy ☺