How would you design the schema for a blog-like site with document-based databases (mongodb). The site has the following objects: User, Article, Comment. User can add Comments to Article. Each User can also vote exactly once per Comment.
I want to be able to do these queries efficiently:
1. get Article A, comments on Article A and # of votes per comments
2. get all comments by User B across all articles
3. get all comments User B voted for
My first attempt is to put articles and comments in separate collections and comment can contain a list of users that voted for it. This makes query 1 and 2 simple. And for 3, I added Vote collection which keep tracks of votes by users.
There's some obvious drawback such as duplicating user vote data and query 1 will take two calls to the database. Is there a better approach?
Article {
"user_id"
}
Comment {
"user_id",
"article_id",
[user_voted],
}
Vote {
"user_id",
"comment_id",
}