Stop 06 · MongoDB Data Modeling & AggregationTopics · 3/8

One-to-Many and Many-to-Many Relationships

Part of the MERN Stack Roadmap

In short

A user has many orders. A student takes many courses, and each course has many students. These relationships appear in every app, and MongoDB gives you a few clean ways to model each.

One-to-many links, like a user with many orders, and many-to-many links, like students and courses, are modelled with embedded arrays or arrays of referenced ids depending on size and access.

The jaadu, as a station master, links coaches to a train engine while passengers holding tickets connect by ribbons to several different trains.

Think of it like this

Trains and passengers

A train has many coaches, always attached to it (one-to-many, few: embed). It carries thousands of passengers over a year, each with a ticket pointing to the train (one-to-many, lots: reference from the child). And passengers travel on many trains while trains carry many passengers (many-to-many).

Watch it happen

Choosing a pattern

Relationship
One-to-many
Few: embed array
Many: child stores parentId
Many-to-many
Small: array of ids
Big or with data: link collection

1/5First ask: how many on each side, and how will you query it?

The big ideas

What you need to know

One-to-few: embed

Small, bounded lists like addresses or phone numbers sit inside the parent document.

One-to-many: child points up

Orders store userId. Query with find({ userId }) and add an index on userId.

Many-to-many: id arrays

Store an array of ids on one side, such as student.courseIds, when lists stay small.

Link collection

When the relationship has its own data (grade, enrolledOn) or is huge, create a separate collection for it.

See it in code

Walk through the code

js
1// One-to-many (lots): each order points to its user2db.orders.insertOne({ userId: priyaId, total: 1200, createdAt: new Date() });3db.orders.createIndex({ userId: 1, createdAt: -1 });4db.orders.find({ userId: priyaId }).sort({ createdAt: -1 });5 6// Many-to-many (small): student keeps course ids7db.students.insertOne({ name: "Arjun", courseIds: [dbmsId, osId] });8db.students.find({ courseIds: dbmsId });   // all students in DBMS9 10// Many-to-many with extra data: a link collection11db.enrolments.insertOne({ studentId: arjunId, courseId: dbmsId, grade: "A", enrolledOn: new Date() });12db.enrolments.createIndex({ courseId: 1, studentId: 1 }, { unique: true });

1/3Orders reference the user. The compound index makes "my orders, newest first" fast.

Three patterns side by side. Pick based on size and on the queries your screens need.

Save yourself the pain

Common mistakes

Oops

Storing every order id inside the user document.

Do this instead

That array grows forever. Store userId on each order instead and query by it.

Oops

Keeping id arrays on both sides of a many-to-many and updating only one.

Do this instead

Store the link in one place, or use a link collection, so the two sides can't disagree.

Your mission

Model a college portal

~30 min

Quick check

A blog user may write thousands of posts. How should you link them?

Go deeper

  1. 1Model students and courses with an enrolments collection and with arrays of ids, and compare.
  2. 2Store the reference on the many side when the list can grow large.
  3. 3Practise fetching both sides of a relationship with queries.

Also part of these roadmaps

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge