Hi, I’m trying to figure out the best way to model data for a feed. Users can follow each other, and a user’s feed is a collection of posts/updates from the users they follow.
Suppose User A follows Users B, C, and D, and suppose we have the following updates:
- Time 1: User D updates profile picture.
- Time 2: User C posts a new video.
- Time 3: User D posts a new picture.
- Time 4: User B posts a new picture.
I have a collection keeping track of the event when each user follows another user (instead of storing an array of followers/following).
{
_id: ObjectId(),
followed_userID: userC,
follower_userID: userA,
timestamp: someTimestamp,
}
It’s easy to extract the list of followers/following by having a indexes on the timestamp & followed_userID and timestamp & follower_userID fields.
If User A wants to view their own feed, what kind of data model should I adopt to be able to get the most recent events from User A’s list of followers.
The naive way would be to get the entire array of users User A follows, and then iterate over each one to get their most recent posts, but this is clearly highly inefficient.
How should I be going about this?