Hi everyone,
I am currently designing my data models for a forum-style application and I’m wondering what the best way to model the data is, specifically for the “Posts” collection. Each “Post” will have “Comments”, and those “Comments” will have a “User” who created the comment. I’ve read the documentation for “One-to-Many relationships with Embedded Documents” and have considered the “Subset Pattern”, however, I’m not sure what the best way to store the “User” information would be and am having a tough time anticipating the required queries to have the user information available for each rendered comment.
I’m currently deciding on the following two options for the “Posts” collection (below are shortened for simplicity here).
The first one stores the entire “User” object on the “Comment” object (i.e. double-nested documents):
{
"_id": "ObjectID(...)",
"Comments": [
{
"comment_id": "",
"text": "",
"user": {
"_id": ""
}
},
{
"comment_id": "",
"text": "",
"user": {
"_id": ""
}
},
...
{
"comment_id": "",
"text": "",
"user": {
"_id": ""
}
},
]
}
The second one stores just the ID of the “User” who created the “Comment” (i.e. reference):
{
"_id": "ObjectID(...)",
"Comments": [
{
"comment_id": "",
"text": "",
"user_id": ""
},
{
"comment_id": "",
"text": "",
"user_id": ""
},
...
{
"comment_id": "",
"text": "",
"user_id": ""
},
]
}
I’m not sure what the best way to approach this problem would be and would really appreciate any sort of guidance.