Document Structure and Relationships
What this domain tests
Document Structure and Relationships tests how you represent 1:1, 1:N, and N:M relationships in BSON documents. Expect questions on embedding versus referencing, parent-child ownership, array growth, and how relationship choices affect atomicity and query count.
Relationship choices
- 1:1: embed when both sides are read together and the child is bounded; reference when the child is large, sensitive, or accessed separately.
- 1:N: embed when N is small and bounded; reference when N is large, using an index on the child's foreign key.
- N:M: reference with arrays on one or both sides, but avoid unbounded arrays on both sides. A separate join collection can be clearer for high-volume relationships.
- Parent-child: embedding gives atomic updates to parent and children in one write; referencing requires multiple writes or a transaction.
Worked example
A blog post can have thousands of comments. Embedding all comments in the post creates an unbounded array and risks the 16 MB limit. Instead, store comments in a comments collection with postId, and index {postId:1, createdAt:-1}. If the UI shows only the latest five comments with each post, use the subset pattern: keep latestComments in the post and the full set in comments. This keeps the common read to one document without unbounded growth.
Traps
Treating embedding as always faster; creating reference cycles; duplicating data without an update strategy; assuming multi-document transactions are free; and forgetting that array growth rewrites the document.