Embedding vs referencing
Understand the idea
A document can hold related data inside it, or it can point at other documents. Each choice has a bill.
The idea
A can embed related values or store an id that resolves later. a profile object is one-to-one: the city comes back with the user. Embedding an addresses array is one-to-many, which is fine while the array stays short. A notes collection that stores authorId is one-to-many by reference: the author name is a second query. Enrollments of studentId and courseId are many-to-many. You do not copy the student onto every course.
What changes
| Relationship | Shape in this lesson | Cost |
|---|---|---|
| One-to-one | profile object on the user | One read, and the object stays small |
| One-to-many, short | addresses array on the user | One read, until the array grows without bound |
| One-to-many, referenced | notes.authorId | A second query to load the author or the notes |
| Many-to-many | enrollment documents of two ids | Each side stays small, and you query the pairs |
The command
Embedded profile and addresses
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Referenced author and notes
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Many-to-many enrollments
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
The mistake
Check the result
The embedded run prints Kathmandu and addresses 2. The referenced run prints Ada and Milk,Eggs. The enrollment run prints course-1,course-2. Each Run uses a new database.
Guided challenge
Embed Kathmandu on the profile
The user profile city is Pokhara. Change it to Kathmandu so the first printed line matches the embedded example in the lesson.
Embed Kathmandu on the profile
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
EXPECTED RESULT
Kathmandu addresses 2
Hints 0/2
Use all hints or make 3 failed checks (0/3).
Free exploration
Change this example and run it again. Each run is a new process. Nothing is saved.
Your variation
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.