A
A
Alex2014-10-31 13:47:23
MongoDB
Alex, 2014-10-31 13:47:23

What is the best way to store post comments in mongodb ?

I master mongodb, here I am interested.
Keep the tree as it is?
Or is there a more productive way?

{
    "_id" : ObjectId("5453533c25cc38377ac6682c"),
    "userId" : ObjectId("54526ed525cc38377ac6682a"),
    "title" : "androguide",
    "content" : "text",
    "preview" : null,
    "route" : "123123123",
    "marker" : false,
    "views" : 98,
    "likes" : 4,
    "status" : {
        "visible" : true,
        "commentsVisible" : true,
        "delete" : false
    },
    "dates" : {
        "createdAt" : ISODate("2014-10-30T23:05:28.718Z")
    },
    "copy" : {
        "shortDescription" : "text"
    },
    "comments" : [ 
        {
            "userId" : ObjectId("54529ac125cc38377ac6682b"),
            "content" : "Мой первый комментарий",
            "createdAt" : ISODate("2014-11-30T11:45:23.718Z"),
            "likes" : 1,
            "delete" : false,
            "reply" : [ 
                {
                    "userId" : ObjectId("54529ac125cc38377ac6682b"),
                    "content" : "Отевет на комментарий",
                    "createdAt" : ISODate("2014-11-30T11:46:23.718Z"),
                    "delete" : false,
                    "reply" : [ 
                        {
                            "userId" : ObjectId("54529ac125cc38377ac6682b"),
                            "content" : "Отевет на комментарий",
                            "createdAt" : ISODate("2014-11-30T11:46:23.718Z"),
                            "delete" : false
                        }, 
                        {
                            "userId" : ObjectId("54529ac125cc38377ac6682b"),
                            "content" : "Отевет на комментарий",
                            "createdAt" : ISODate("2014-11-30T11:46:23.718Z"),
                            "delete" : false
                        }
                    ]
                }, 
                {
                    "userId" : ObjectId("54529ac125cc38377ac6682b"),
                    "content" : "Отевет на комментарий",
                    "createdAt" : ISODate("2014-11-30T11:46:23.718Z"),
                    "delete" : false
                }
            ]
        }, 
        {
            "userId" : ObjectId("54529ac125cc38377ac6682b"),
            "content" : "Мой второй комментарий",
            "createdAt" : ISODate("2014-11-30T11:46:23.718Z"),
            "delete" : false
        }
    ]
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
anyd3v, 2014-10-31
@anyd3v

Store in a separate collection. If you store in one object, then you need to monitor the size of the record, in mongo the size of one record cannot exceed 16 mb.

L
lega, 2014-11-01
@lega

It depends on how you would use them (display).
If you display all of them only together with the document, then your option is suitable. With one request you get both posts and comments.
If you need to use comments somewhere else: the last 5 comments, top comments, aggregation by comments, or if you need to link to specific comments somewhere, then it makes sense to store them in a separate collection (and cache the post a little).

N
Nikolai, 2014-10-31
@j_wayne

Sometimes it makes sense to store a certain number of recent posts with a document, say the 5 most recent ones. Then the article and the latest comments can be spit out in one request. And the rest of the comments, yes, in a separate collection.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question