Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced18 min

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, where it lives, and what you pay
RelationshipShape in this lessonCost
One-to-oneprofile object on the userOne read, and the object stays small
One-to-many, shortaddresses array on the userOne read, until the array grows without bound
One-to-many, referencednotes.authorIdA second query to load the author or the notes
Many-to-manyenrollment documents of two idsEach 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.

    01When is embedding a good fit?
    02What does a many-to-many enrollment collection store?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson