F
F
floppa3222018-11-05 20:30:06
MongoDB
floppa322, 2018-11-05 20:30:06

Array of embedded documents vs Foreign key?

There are 2 collections: Users and Comments. Users has N documents, and Comments is nested within Users (that is, Users = {..., [Comments], ...} - an array of comments) and has an average of M documents for each Users document. Comments have an indexed Views field.
It is required to find all comments that have, say, 200 views.
Complexity for each of 2 approaches:
1.Comments is embedded in Users as described above, then complexity will be N*LogM. That is, you need to look at each user - N iterations, and then go over the Views tree - LogM
2.Comments exists autonomously and its docks have a link - ObjectID to the dock. Users (classic one-to-many). Then Comments will have N * M docs and the complexity will be Log( N * M ) .
Conclusion: if you want to filter by the fields of nested docs, then you should implement collections not as nested ones, but as a separate collection, as in RDB.
Am I estimating the difficulty correctly? If so, what are the use cases for arrays of nested documents before the RDB approach described in point 2?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lega, 2018-11-05
@lega

Conclusion: if you want to filter by the fields of nested docs, then you should implement collections not as nested ones, but as a separate collection, as in RDB.
More often yes. There are also options such as marking certain users so as not to sort through all, or duplicating "distinctive" comments into a separate collection, or vice versa duplicating into a "user document", in nosql it is more flexible.
N * LogM. That is, you need to look at each user - N iterations
In order not to do N iterations, an index is used to get the necessary documents without enumerations (and enumerations use the "disk").
Also here you need to look at how you will change the "number of views" field, if the comments are in a separate collection - then it will be easier and faster.
Comments will have N * M docs and the complexity will be Log( N * M ).
Where did you get Log from? To get "all comments with 200 views.", the collection of users does not need to be touched at all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question