Câu hỏi cốt lõi: dữ liệu con nên nằm bên trong document cha (embed) hay nằm ở collection riêng rồi trỏ tới (reference)?
Nên Embed (nhúng) khi:
- Dữ liệu con thuộc hẳn về cha và thường đọc cùng nhau.
- Số lượng con nhỏ và có giới hạn.
- Ít khi cần update con một cách độc lập.
Nên Reference (tham chiếu) khi:
- Dữ liệu con lớn hoặc tăng không giới hạn.
- Quan hệ nhiều-nhiều.
- Cần query con độc lập.
- Có nguy cơ vượt giới hạn 16MB của một document.
Ví dụ embed vài địa chỉ vào user (số lượng nhỏ):
{
_id: ObjectId("..."),
email: "a@example.com",
addresses: [{ city: "HCM", type: "shipping" }]
}Lưu ý: thiết kế MongoDB nên bám theo cách bạn truy vấn dữ liệu (query pattern), đừng bê nguyên 1-1 sơ đồ ERD của DB quan hệ sang.
The core question: should child data live inside the parent (embed) or in a separate collection that you point to (reference)?
Embed when:
- Child data belongs entirely to the parent and is usually read together.
- The count is small and bounded.
- It rarely needs independent updates.
Reference when:
- Child data is large or grows without bound.
- It is a many-to-many relationship.
- You need to query the children independently.
- There is a risk of exceeding the 16MB document limit.
Example embedding a few addresses in a user (small count):
{
_id: ObjectId("..."),
email: "a@example.com",
addresses: [{ city: "HCM", type: "shipping" }]
}Note: MongoDB design should follow how you query the data (query patterns); don't copy a relational ERD 1-to-1.