I am fairly new to MongoDB document-based storage, and my background is in tabular databases and I am having trouble understanding the right way to model this relationship.
Currently I have a collection called users, I would like to add a structure for posts, which are owned by users, and on these posts users can leave nested comments. Based on the documentation I read I believe it would end up looking something like this:
users: [
{
userId: 1,
username: 'baconater',
posts:[
postId: 1,
body: 'some user created content',
comments: [
{
commentId: 1,
comment: 'a comment',
userId: 2, //I don't know how to properly relate this back to a user
comments: [ //replies to comments go here? can I arbitrarily nest them??
... similar structure to the parent comment object
]
},
{
userId: 2,
username: 'pwnyoursocks',
posts:[
postId: 2,
body: 'some moew user created content',
comments: [
{
commentId: 1,
comment: 'a comment',
userId: 1,
comments: []
}
]
]
}
]
This structure is user-centric which is good for user management, but then how would I do something non-user-centric like aggregating posts by points? Also what is the correct way to relate objects back to users, such as in the comments which, while nested in a specific user, reference the user who created the comment who is probably different than the parent user?