Is it embed or reference in the explanations?

Hi in the chapter 2 lecture: one-to-many relationship at minute 2:15 it is shown at the picture that top_reviews it is embed but inside it has the user_id and user_name so i have a question is this information embed or reference or where this information is appering

Hey @Jurgen_Ruegenberg_Buezo,

top_reviews is embedded in the items document. user_id, user_name, date and body are the fields inside the top_reviews array of subdocuments. They first two are denoting the id and name of the person who gave a review of that item. It would have been a reference if top_reviews would have been a separate document with a field named item_id which would have been used to denote the reference to that item.

Let us know if there’s any confusion regarding this. Feel free to reach out for anything else as well.

Regards,
Satyam

2 Likes

So to declare a link i must use an item_id for both collections to been able to create that relationship right?

Hey @Jurgen_Ruegenberg_Buezo,

Yes. For this to be a reference, there should have an item_id in the top_reviews document, so as to be able to link which item the review is for. In that case, items and top_reviews will be two separate documents.

Regards,
Satyam

1 Like

“Embedding” and “Referencing” have their pros and cons.

You can “embed” things into a document but if the embedded docs are increasing in numbers, the main document’s size will bloat up.

On the other hand, referencing keeps the size small, but it costs more time to fetch referenced documents; in this case, trying to “lookup” all reviews for every item as happens in traditional relational database “joins”

In the model in question, we have an intermediate solution by “embedding” only the top 20 reviews into an item document, so when fetching items we do not spend much time. we then “reference” an item in a review document, so when you want reviews of a single item, we will fetch only from reviews.

the same would go for the user field in a review: embed basic info of the user along with a reference to full info in “users” collection.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.