MongoDB.live, free & fully virtual. June 9th - 10th. Register Now MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

Data modeling for social media followers/following... bucket pattern?

Hi, I’m working on implementing social functionality in an app where users can follow each other. I’m trying to come up with a good approach for storing these follower/following relationships between users.

I was initially going to use two collections… Follower and Following where each document would contain a user id, and an array of users the person is following, or who are following them.

My concern with this approach is that if follower count gets very large, we will hit the 16mb BSON limit. An alternative would be having one document per follower relationship, which introduces other performance issues.

Is a bucket pattern the best solution for this… or is there a better approach? I could have buckets of 100 followers and then also use those buckets for paginating lists of followers to display.

However my concern is that when deleting a follower… all buckets for a given user would have to be searched and then when the follower is removed, an old bucket might have a gap. This gap could then be filled by a new follower in the future, but this would mean it would be more difficult to display the followers in order of new to old without sorting on date followed, which I imagine could become a performance issue.

I’m relatively new to MongoDB and learning. This great article by @Justin mentions that this bucket gap issue can be solved by expressive update instructions introduced in MongoDB 4.2, but I’m having trouble understanding how this would resolve the issue:

Any suggestions for using MongoDB when dealing with potentially large and ever growing social data like this?

Following up to see if anyone has any suggestions on this.

To summarize the questions above, is there a recommended approach or pattern for handling large amounts of follower/following relationships between users in a social media context using MongoDB?

I found the answer here referencing the socialite project:

For anyone else who comes across this, it looks like the suggested approach is to store each follow relationships as one individual document per relationship. So, two collections as noted above, but no bucket pattern, just individual follower or following documents.