for learning purposes, i am trying to reconstruct some TikTok functionalities within my MERN App.
Some requirements i would like to implement are:
A many-to-many relationship between posts and hashtags: → A user can create a post and add multiple hashtags → A user can search for a specific hashtag and get posts sorted by timestamp or "likesCount"
Follow/-Unfollow System
Like/-Unlike System
The Design Pattern should avoid following limitations:
- 16 MB Document Size Limitation
- Slow Performance when paginating due to "limit" and "skip" restrictions
- Anything else?
I`ve read about the "Bucket Pattern" (https://www.mongodb.com/blog/post/paging-with-the-bucket-pattern--part-1) and the "Outlier Pattern" (https://www.mongodb.com/blog/post/building-with-patterns-the-outlier-pattern).
When i take a look at TikTok's query responses, it looks like they have implemented the "Outlier Pattern" for retrieving their content.
Whether it's in the feed, where the users is only displayed personalised data or in the hashtag "page", where the data is sorted by the "likesCount"
Query Response when fetching the Feed
Unfortunately, I can't find anything on the internet about the "Outlier Pattern", which makes it difficult for me as a beginner to think my way into this pattern and to implement it correctly with MongoDB.
Like it's described in this article above, i obviously need a document with a structure like this:
{
"_id": ObjectID("507f191e810c19729de860ea"),
"hashtagName": "football",
"hashtag_posts": ["post01", "post02", "post03", ..., "post50"],
"hasMore": true
}
{
"_id": ObjectID("507f191e810c19729de860eb"),
"hashtagName": "football",
"hashtag_posts": ["post51", "post51", "post52", ..., "post100"],
"hasMore": false
}
My questions are:
- How do i sort my list of posts based on attributes like "likesCount" if only a certain amount of data gets saved into a documents array? What about the second document with the other 50 posts?
- How do i get the count attribute of a hashtags posts?
My Mongoose Schema for a Post looks like this:
const postSchema = new Schema(
{
desc: {
type: String,
required: true
},
hashtags: [
{
type: [Schema.Types.ObjectId],
ref: 'Hashtag'
}
],
stats: {
likesCount:{
type: Number,
required: true,
default: 0
},
}
});