A
A
Anton2015-09-04 22:09:57
JavaScript
Anton, 2015-09-04 22:09:57

How to make nested comments in mongodb?

I use a meteor. You need to nest comments. There is a collection of comments that contains all comments with infinite nesting (replies to comments). Is this architecture normal? How to attach a new comment to the right place? And how to count all the comments given the replies?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lega, 2015-09-05
@SPAHI4

How to attach a new comment to the right place?
Looks like a link to a parent comment.
And how to count all the comments given the replies?
In each comment, store a link to the post / article, so all the comments of the post can be taken out with one request, and when adding a comment, increment the "number of Comments" in the post document, this way you will have the number of comments on the post without downloading these same comments.
And if you don’t directly refer to comments (sometimes this happens), then you can shove all comments into one document if limits allow.
Example, post:
{
post_id: 'p1',
text: '...'
}
Comment at root
{
comment_id: 'c1'
parent_comment: null,
post_id: 'p1',
text: '...'
}
Child comment:
{
comment_id: 'c2'
parent_comment: 'c1',
post_id: 'p1',
text: '...'
}
Add a child comment to comment c2:
db.comment.insert({parent_comment: 'c2', post_id: 'p', text: '. ..'})
Selecting all comments on a post:
db.comment.find({post_id: 'p1'}) Build a tree of comments
from the resulting list.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question