Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (47.6k points)

I'm new to MongoDB--coming from a relational database background. I want to design a question structure with some comments, but I don't know which relationship to use for comments: embed or reference?

Do I have to use ref not embed? Then I have to create a new collection for comments?

2 Answers

0 votes
by (106k points)

Embed is good if you have one-to-one or one-to-many relationships between entities, and reference is good if you have many-to-many relationships.

0 votes
by (108k points)

While dealing with unstructured data, in MongoDB, embedding may be better for all the instances where there is a strong correlation between the data. If something feels strange while shaping the data with embedding (such as relationships that result in data duplication under multiple top-level objects), you can switch to referencing. Referencing should be used more delicately as there are restrictions to referenced documents and references are less effective as compared to the embedded.

Related questions

Browse Categories

...