Answer the question
In order to leave comments, you need to log in
Mongo: storing comments and likes in a separate collection or in attributes of the file itself?
And again, hello everyone (and prosper).
When developing the structure, I came across one question. Suppose there is a photo on the site that will have likes and comments. Accordingly, there is a collection "Photo". Presumably, it is possible to store all the data in the collection itself, creating a structure:
"likes" : [
{"user" : ObjectID,
"datetime" : datetime.utcnow()}],
"comments" : [
{"user" : ObjectID,
"datetime" : datetime.utcnow(),
"comment": "Бла-бла-бла"}]
Answer the question
In order to leave comments, you need to log in
If you are 100% sure that the size of the document will not exceed the limit (if I'm not mistaken, it's 16mb), then storing it as a nested array will obviously be more convenient. Otherwise, in a separate collection. But if stored separately, you need a cache of the total number of likes, so you will need to rivet this logic.
The advantage of separate storage is that it is more convenient to make mass updates if they are required. With nested storage, the number of calls to the database is reduced.
If the likes and comments of this photo will be taken only for this photo, for this page. Let it be all together.
If, for example, you have a feed of fresh likes or comments, i.e. if they are used somewhere else, and even in a different sorting, with different likes, then a separate collection is needed (although for optimization it will be possible to cache/duplicate in photos).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question