0
0
MongoDBquery~5 mins

Many-to-many with references in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a many-to-many relationship in databases?
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
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
What field type is commonly used to store references in MongoDB?
ANumber
BArray of ObjectIds
CBoolean
DString
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
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
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.