1

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?

Tulains Córdova
  • 39,201
  • 12
  • 97
  • 154
tt9
  • 611
  • 7
  • 19
  • http://blog.mongodb.org/post/87892923503/6-rules-of-thumb-for-mongodb-schema-design-part-2 – Michael Green Aug 09 '16 at 04:27
  • Are you dead set on Mongo? Sounds like a relational db would better fit your needs. Or you could try something like [OrientDB](http://orientdb.com/orientdb/) which is a Mongo-like document store, but also supports references. – paj28 Aug 09 '16 at 15:38
  • @paj28 Its more of a learning experience thing. I would like to know how to use mongo for this sort of thing. Even if I started with an app that is better suited for document over tabular, it isn't out of the realm of possibility that something like this would need to be devised later on in the app's development, and if that happened I would like to know the best way to structure my data. I would assume there is something out there. – tt9 Aug 09 '16 at 19:10
  • Ok, I would make users and posts separate collections. posts has a [reference](https://docs.mongodb.com/manual/reference/database-references/) to users. comments is a list within posts, with a reference to users (just like you have already). You can query posts by userid, and you will need an [index](https://docs.mongodb.com/manual/indexes/) to make that efficient. Good luck :) – paj28 Aug 09 '16 at 19:26

0 Answers0