Follow Action Schema for Mongo DB

Schema Design for Twitter like Follow Action with MongoDB


This has been asked so many times on stack overflow and other tech blogs. And many already have implemented this very effectively. Just consider it YASS “Yet Another Schema Solution”.

I am not going to talk much here as i am in middle of building something. So just short solutions.


All the schemas are separate collection, you can name it follow or what the fuck if you like it that way. Absence of end field indicate 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 : ‘data’,
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 : ‘data’,
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 : ‘data’,
end : ‘date’
}

Well that’s it. Enjoy ☺

Email me when Ravi Suhag publishes or recommends stories