I design a MongoDB collection where I am planning to use mongo ObjectID as a Document name. The ObjectID is a PostID, I want to track all the userID who liked or disliked the post.
Hi @Kellen,
Welcome to MongoDB Community,
If I understood correctly you are looking for a design pattern to store those likes per post?
If my assumption is correct I think that storing all users that might like a post in the post document is risky , as you might have an unbounded array and potentially reach 16MB limit.
What you should keep in the post document the number of likes/comments that the post have:
{
"_id" : ...,
"title" : ...,
likes : 100,
comments: 10
...
}
Those should be updated as your likes are inserted.
What I think you should consider is 2 options:
- A small document per like in a userLikes collection:
{
_id : ... ,
postId : ... ,
userId : ... ,
userName : ... ,
avatarLink ...
}
- A likes collection where you will use the outlier pattern to keep the users that like the post in bucketed arrays.
{
_id : ... ,
postId : ... ,
arraySize : 50,
users : [ {
userId : ... ,
userName ... ,
avatarLink ...
],
hasNext : true
}
I suggest to read the following blogs:
- https://www.mongodb.com/article/schema-design-anti-pattern-summary/
- https://www.mongodb.com/article/mongodb-schema-design-best-practices/
- Building with Patterns: A Summary | MongoDB Blog
Please let me know if that makes sense.
Best regards,
Pavel
1 Like