This pattern helps connect one item to many related items in different collections. It keeps data organized and easy to update.
You have a list of books and each book has many reviews.
A blog post with many comments.
A customer with many orders.
A teacher with many students.
A playlist with many songs.
Syntax
MongoDB
/* Store references (IDs) of many related documents in an array */
{
_id: ObjectId("..."),
name: "Parent Item",
relatedItems: [ObjectId("..."), ObjectId("..."), ...]
}
The parent document holds an array of IDs pointing to many child documents.
This keeps data separate but linked, so you can find related items easily.
Examples
The author document stores IDs of books they wrote.
MongoDB
/* Example: Author with many books */
{
_id: ObjectId("507f1f77bcf86cd799439011"),
name: "Jane Doe",
books: [ObjectId("507f1f77bcf86cd799439012"), ObjectId("507f1f77bcf86cd799439013")]
}
Each book is stored separately with its own details.
This example shows how an author references many books by their IDs. Then we find the author and get all their book titles by looking up the books collection.
C. { _id: 1, title: 'Post', comments: 'This is a comment' }
D. { _id: 1, title: 'Post', comments: ObjectId('abc123') }
Solution
Step 1: Identify referencing format
One-to-many referencing stores an array of ObjectIds pointing to related documents.
Step 2: Check each option
{ _id: 1, title: 'Post', comments: [ObjectId('abc123'), ObjectId('def456')] } uses an array of ObjectIds, which is correct. Others embed data or use a single ID incorrectly.
Hint: Use array of ObjectIds to reference many documents [OK]
Common Mistakes:
Embedding comment text instead of IDs
Using a single ObjectId for many comments
Storing comments as plain strings
3. Given these two collections: posts: [{ _id: 1, title: 'Hello', comments: [101, 102] }] comments: [{ _id: 101, text: 'Nice!' }, { _id: 102, text: 'Great!' }] What will be the result of this query to find comments for post with _id 1? db.comments.find({ _id: { $in: db.posts.findOne({ _id: 1 }).comments } })
medium
A. [] (empty array)
B. [{ _id: 1, title: 'Hello', comments: [101, 102] }]
Find comments by IDs in post = matching comment docs [OK]
Hint: Use $in with IDs array to get related documents [OK]
Common Mistakes:
Expecting post document instead of comments
Thinking nested query causes syntax error
Getting empty result by mistake
4. You have this post document: { _id: 1, title: 'Test', comments: ['abc', 'def'] } But when you run db.comments.find({ _id: { $in: post.comments } }), it returns no results. What is the most likely problem?
medium
A. ObjectIds in post.comments are strings, not ObjectId type
B. The comments collection has no documents with those ObjectIds
C. The query syntax is incorrect for $in operator
D. The post document is missing the comments field
Solution
Step 1: Check data types of referenced IDs
If post.comments contains strings instead of ObjectId objects, the query won't match.
Step 2: Confirm query correctness
The query syntax is correct, so the mismatch is likely due to type difference.
Final Answer:
ObjectIds in post.comments are strings, not ObjectId type -> Option A
Quick Check:
Type mismatch in IDs causes no matches [OK]
Hint: Ensure referenced IDs are ObjectId type, not strings [OK]
Common Mistakes:
Assuming empty results mean syntax error
Ignoring data type differences
Thinking missing field causes this error
5. You want to design a blog system with users, posts, and comments. Each user can write many posts, and each post can have many comments. Using one-to-many referencing, which is the best way to organize references?
hard
A. comments store userId; posts store arrays of commentIds
B. posts store userId; comments store postId; users do not store references
C. users embed all posts; posts embed all comments
D. users store arrays of postIds; posts store arrays of commentIds
Solution
Step 1: Understand one-to-many referencing best practice
Usually, the 'many' side stores the reference to the 'one' side to avoid large arrays.
Step 2: Apply to blog system
Posts store userId (one user, many posts), comments store postId (one post, many comments), users don't store arrays.
Final Answer:
posts store userId; comments store postId; users do not store references -> Option B
Quick Check:
Many side stores reference to one side [OK]
Hint: Store reference on 'many' side to 'one' side document [OK]
Common Mistakes:
Embedding all posts or comments inside users or posts
Storing large arrays on 'one' side documents
Confusing which side stores the reference
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.