Modelling Data for a Forum-style app

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.

Hi @Shadee_Merhi ,

You are correct the main insentive on how to store data should be derived from what information you need to query together to form an application page or view.

In your case I would actually consider the following model for this where you seperate the data into three collections :

  • Users
{ _id ...,
Username : ...,
Email : ....
...
}
  • Posts
{
_id ... ,
Title ... ,
Content ... ,
User : { 
         Username : ...,
         UserId : ... ,
         Avatar : ... 
},
...
}
  • comments
{ 
_id ...,
PostId : ... ,
User : { 
         Username : ...,
         UserId : ... ,
         Avatar : ... 
}
Content : ....,
...
}

The idea behind this is when a user registered you create a users document.

If you want to show posts to a user you query posts based on a criteria or limit.

If you click a post only than you query an index query on postId from comments.

Each post or comment has its author information which can be displayed as part of each document.

I suggest to read

Thanks
Pavel

Thank you for the reply, Pavel. This is very helpful!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.