E
E
Exxplozer2014-08-05 20:30:56
MongoDB
Exxplozer, 2014-08-05 20:30:56

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

1 answer(s)
M
Maxim Vasiliev, 2014-08-05
@Explozer

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 } }
.
Sorting in linearization order by means of mongo will not work, unless you enter an additional tricky field for it. For example, string concatenation level of all ancestors.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question