Data Modeling
Many-to-Many Relationships
Products and tags, students and courses — two collections and an array of ids, or a join collection.
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"] }{ name: "Rahul", courseIds: [ObjectId("…"), ObjectId("…")] }
// Or ids on both if both reads need the list and both stay small{
productId: ObjectId("…"),
userId: ObjectId("64a1b2c3d4e5f6a7b8c90001"),
rating: 5,
comment: "Daily driver.",
createdAt: ISODate("2026-08-20")
}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?
If the other side is a small label, embed it — tags on a product. If both sides need a short list of ids, store the array on the document you read. If the relationship has its own fields or either list is unbounded, use a linking collection with two foreign keys, like reviews with userId and productId.