Follow action schema for Mongo DB

Schema design for twitter like follow action with MongoDB

Ravi Suhag
Jun 28, 2014 · 2 min read

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 ☺

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store