I'm building a social network very similar to Twitter, in which users can see a feed with content posted by users they follow. Users can like and re-post (i.e. "Retweet") posts. Re-posted content works just like it does on Twitter. If a user re-posts a post, it will get shown to their followers.

My database is mongo db. Posts go into a posts collection. But I'm wondering about the best way to include a re-post so that it gets queried along with the posts for the feed query. I've thought about doing it several ways:

  • Insert a re-post doc in posts docs collection, but add a field referencing the clip being re-posted. Not sure whether to repeat the re-posted post's data here.
  • Use a separate collection for re-posts. Query both the posts and re-posts collections when retrieving the feed.
  • Don't insert a re-post doc at all. Include a sub-document in the post's document that includes the re-poster info. When querying for the feed, use re-posters as part of the criteria. Problem is, the application will have no way to know if the post was selected bc the user follows the author or a re-poster, or which reposter.

Not sure what the best practice is for this. Advice is much appreciated.

| improve this question | | | | |
  • 1
    This is not necessarily directly related to your question, but you may want to have a look at this blog post on "social inbox" schema designs in MongoDB. blog.mongodb.org/post/65612078649/… – helmy Oct 31 '14 at 21:33
  • For no3 document and sub-document will have an _id that will included on the re-post so you can track the originator. What is scary for no3 is the size of the document might become huge. I would combine no2 and no3. A sepetate collection for the subdocs of no3 – Antonios Nov 1 '14 at 16:02

For anyone who's curious, I ended up doing it the first way: Insert a re-post doc in posts docs collection. The doc inserted doesn't contain any of the re-posted doc's info, just a reference to its ID under a "reposted_id" field. It also includes info about the reposter.

The query for the feed will match either the author ID or the reposter ID. From the results of that query, we remove duplicate posts; i.e. if the user follows both the original author and the reposter, or multiple reposters, they should only see the post once. Then the post info is queried for each unique re-posted post.

| improve this answer | | | | |
  • hi, im trying to understand the logic about the first aproach (i think is the best way too). But how do you manage it? i mean, when a user click on the repost or share post button, that post get the userid and create the same post but with that id?so, it takes the postdata and create another one with the userid? @inorganik – Alex Hunter Dec 17 '19 at 13:18
  • Reposted posts in my app were stored with some user meta data of the original poster and the reposter (username, avatar url, etc) and the id of the original post stored under the field "reposted_id" - this way the original post can always be referenced. So it could be reposted or shared, we could find the original post by that id. – inorganik Dec 17 '19 at 15:25
  • ok thank you, if you can share your schema will be great @inorganik – Alex Hunter Dec 17 '19 at 15:40
  • That is the schema, not sure what you want me to share? – inorganik Dec 17 '19 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.