A many-to-many relationship means that multiple records in one collection can relate to multiple records in another collection. For example, many students can enroll in many courses.
Click to reveal answer
beginner
How do MongoDB references help model many-to-many relationships?
MongoDB uses references by storing the IDs of related documents in arrays. This way, documents can point to many others without duplicating data.
Click to reveal answer
intermediate
Why use references instead of embedding for many-to-many relationships?
References avoid data duplication and keep documents smaller. Embedding many related documents can make documents too large and harder to update.
Click to reveal answer
beginner
Example: How to store course IDs in a student document for many-to-many?
In the student document, include a field like enrolledCourses which is an array of course IDs. Each ID references a course document.
Click to reveal answer
intermediate
What is a common pattern to query many-to-many references in MongoDB?
First, find the document with the array of IDs, then use those IDs to query the related collection. This is called manual referencing or client-side join.
Click to reveal answer
In MongoDB, how do you represent a many-to-many relationship?
ABy creating a single document for all relationships
BBy embedding all related documents inside one document
CBy storing arrays of referenced document IDs in both collections
DBy using SQL JOIN statements
✗ Incorrect
MongoDB uses arrays of referenced IDs in documents to represent many-to-many relationships.
Why might embedding not be ideal for many-to-many relationships?
AIt duplicates data and can make documents too large
BIt is faster than referencing
CIt automatically updates related documents
DIt is required by MongoDB
✗ Incorrect
Embedding duplicates data and can cause large documents, which is not efficient for many-to-many.
What field type is commonly used to store references in MongoDB?
ANumber
BArray of ObjectIds
CBoolean
DString
✗ Incorrect
References are stored as arrays of ObjectIds pointing to related documents.
How do you retrieve related documents in a many-to-many setup in MongoDB?
AUse SQL JOIN to combine collections
BUse map-reduce functions only
CEmbed all related documents in one query
DQuery the IDs stored in the array and fetch related documents separately
✗ Incorrect
You query the array of IDs and then fetch related documents in separate queries.
Which of the following is NOT a benefit of using references for many-to-many?
AAutomatically enforces foreign key constraints
BKeeps documents small
CAvoids data duplication
DAllows flexible relationships
✗ Incorrect
MongoDB does not automatically enforce foreign key constraints with references.
Explain how to model a many-to-many relationship using references in MongoDB.
Think about how two collections can point to each other using IDs.
You got /4 concepts.
Describe the steps to query related documents in a many-to-many relationship with references in MongoDB.
Remember MongoDB does not do automatic joins like SQL.
You got /4 concepts.
Practice
(1/5)
1. What does a many-to-many relationship with references mean in MongoDB?
easy
A. Only one collection has references, the other does not.
B. One collection stores all data, the other is empty.
C. Both collections store arrays of IDs pointing to each other.
D. Data is duplicated in both collections without IDs.
Solution
Step 1: Understand many-to-many references
In MongoDB, many-to-many means each document in one collection can link to many documents in another, and vice versa.
Step 2: Identify how references are stored
This is done by storing arrays of IDs in both collections, so each side knows its related documents.
Final Answer:
Both collections store arrays of IDs pointing to each other. -> Option C
Quick Check:
Many-to-many references = arrays of IDs in both collections [OK]
Hint: Many-to-many means both sides hold arrays of IDs [OK]
Common Mistakes:
Thinking only one side stores references
Confusing many-to-many with embedding all data
Assuming data is duplicated without IDs
2. Which of the following is the correct way to store references for a many-to-many relationship between students and courses collections in MongoDB?
easy
A. { _id: 1, name: 'Alice', courses: 101 } and { _id: 101, title: 'Math', students: 1 }
B. { _id: 1, name: 'Alice', courses: [101, 102] } and { _id: 101, title: 'Math', students: [1, 2] }
C. { _id: 1, name: 'Alice', courses: 'Math' } and { _id: 101, title: 'Math', students: 'Alice' }
D. { _id: 1, name: 'Alice', courses: [] } and { _id: 101, title: 'Math', students: [] }
Solution
Step 1: Check reference format
References must be arrays of IDs to represent many-to-many links.
Arrays of IDs in both collections = correct syntax [OK]
Hint: Use arrays of IDs, not single values or names [OK]
Common Mistakes:
Using single ID instead of array for many-to-many
Storing names instead of IDs as references
Leaving reference arrays empty without reason
3. Given these two documents: { _id: 1, name: 'Bob', courses: [201, 202] } { _id: 201, title: 'History', students: [1, 3] } What will be the result of querying the courses array for student with _id: 1?
medium
A. ['History']
B. [1, 3]
C. []
D. [201, 202]
Solution
Step 1: Identify the student's courses field
The student document with _id 1 has a courses array: [201, 202].
Step 2: Understand the query result
Querying the courses array for this student returns the IDs [201, 202], not titles or student IDs.
Final Answer:
[201, 202] -> Option D
Quick Check:
Student's courses array = [201, 202] [OK]
Hint: Query returns stored IDs, not linked document fields [OK]
Common Mistakes:
Confusing course IDs with student IDs
Expecting course titles instead of IDs
Assuming empty array if unsure
4. You have these two collections: students: { _id: 2, name: 'Eve', courses: [301] } courses: { _id: 301, title: 'Science', students: [] } What is the problem with this many-to-many reference setup?
medium
A. The students array in courses is missing the student ID.
B. The courses array in students is empty.
C. The student document is missing the courses field.
D. The course document has an invalid _id.
Solution
Step 1: Check references in both collections
The student document lists course 301, but the course document's students array is empty.
Step 2: Identify missing reciprocal reference
For many-to-many, both sides must reference each other. The course's students array should include student ID 2.
Final Answer:
The students array in courses is missing the student ID. -> Option A
Quick Check:
Both sides must reference each other [OK]
Hint: Both sides must list each other's IDs [OK]
Common Mistakes:
Ignoring missing reciprocal references
Assuming one side is enough
Confusing empty arrays with missing fields
5. You want to find all courses taken by students who are enrolled in both course 401 and 402. Given collections with many-to-many references, which MongoDB aggregation pipeline stage helps find students enrolled in both courses?
hard
A. { $match: { courses: { $all: [401, 402] } } }
B. { $match: { courses: { $in: [401, 402] } } }
C. { $match: { courses: 401 } }
D. { $match: { courses: { $size: 2 } } }
Solution
Step 1: Understand the query goal
We want students enrolled in both courses 401 and 402, so their courses array must contain both IDs.
Step 2: Choose correct MongoDB operator
The $all operator matches arrays containing all specified elements, perfect for this case.