Answer the question
In order to leave comments, you need to log in
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
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.
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).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question