Many-to-many with references helps connect two groups of things where each item in one group can link to many items in the other group, and vice versa.
When you have students and courses, and each student can take many courses, and each course can have many students.
When you have books and authors, and each book can have multiple authors, and each author can write multiple books.
When you have users and roles, and each user can have many roles, and each role can belong to many users.
Syntax
MongoDB
In MongoDB, use arrays of ObjectId references in both collections.
Example:
{
_id: ObjectId,
name: String,
relatedIds: [ObjectId, ObjectId, ...]
}
Use ObjectId to reference documents from another collection.
Store arrays of these ObjectId in each document to link many items.
Examples
Each student lists courses they take, and each course lists students enrolled.
This example creates two students and two courses. Each student has a list of course IDs, and each course has a list of student IDs. Then it finds all courses Alice takes by looking up her course IDs.
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.