Answer the question
In order to leave comments, you need to log in
What is the correct structure in MongoDB?
As I understand it, in MongoDB, if for example there are posts and comments to them, is it better to use two collections separately? So the structure is similar to relational databases, and in what cases should you nest in collections? And how to properly organize the structure in MongoDB.
Answer the question
In order to leave comments, you need to log in
Subdocuments make sense if there are several of them and there is no need to select within this sub-collection.
For example, pictures attached to the comment.
Comments are potentially numerous and there may be a need for page-by-page fetching, or hiding long branches (like LJ or on d3)
An ordered hierarchical structure (with optimized fetching and expensive permutation) can be made into three fields:
parent: id of the parent comment.
ancestors: id of all ancestors from root to parent.
level: level (length of ancestors)
Selection of any branch, starting from threadid to maxdepth depth, is performed by a query
{ 'ancestors': threadid, 'level': { '$gt': maxdepth } }
. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question