Skip to content

Data Modeling

Many-to-Many Relationships

Products and tags, students and courses — two collections and an array of ids, or a join collection.

IntermediateAbout 6 minutes

Many-to-many means each side has many of the other. You still do not start with a SQL join table. Start with: which side is the hot read, is the relationship just an id list, or does it have its own fields?

{ name: "Mechanical Keyboard", price: 4500, tags: ["electronics", "peripherals"] }

// Rahul's interests — same idea
{ name: "Rahul", interests: ["tech", "gaming"] }
Tags — small, displayed on the product, not a first-class entity
{ name: "Rahul", courseIds: [ObjectId("…"), ObjectId("…")] }

// Or ids on both if both reads need the list and both stay small
Ids on the side you load anyway — if the list stays bounded
{
  productId: ObjectId("…"),
  userId: ObjectId("64a1b2c3d4e5f6a7b8c90001"),
  rating: 5,
  comment: "Daily driver.",
  createdAt: ISODate("2026-08-20")
}
Relationship has payload — its own collection (reviews)

A linking collection is justified when the edge has data (rating, enrolledAt, role) or either id list would be unbounded (every user who ever bought a keyboard). Index { productId: 1, createdAt: -1 } and { userId: 1 }. Two arrays of thousands of ids on both products and users is the shape that later needs a rewrite.

Interview question

How do you model many-to-many in MongoDB?

Think about it first.