0
0
MongoDBquery~5 mins

Many-to-many with references in MongoDB

Choose your learning style9 modes available
Introduction

Many-to-many with references helps connect two groups of things where each item in one group can link to many items in the other group, and vice versa.

When you have students and courses, and each student can take many courses, and each course can have many students.
When you have books and authors, and each book can have multiple authors, and each author can write multiple books.
When you have users and roles, and each user can have many roles, and each role can belong to many users.
Syntax
MongoDB
In MongoDB, use arrays of ObjectId references in both collections.

Example:
{
  _id: ObjectId,
  name: String,
  relatedIds: [ObjectId, ObjectId, ...]
}
Use ObjectId to reference documents from another collection.
Store arrays of these ObjectId in each document to link many items.
Examples
Each student lists courses they take, and each course lists students enrolled.
MongoDB
/* Student document */
{
  _id: ObjectId("507f1f77bcf86cd799439011"),
  name: "Alice",
  courseIds: [ObjectId("507f1f77bcf86cd799439021"), ObjectId("507f1f77bcf86cd799439022")]
}

/* Course document */
{
  _id: ObjectId("507f1f77bcf86cd799439021"),
  title: "Math",
  studentIds: [ObjectId("507f1f77bcf86cd799439011"), ObjectId("507f1f77bcf86cd799439012")]
}
Authors reference books they wrote, and books reference their authors.
MongoDB
/* Author document */
{
  _id: ObjectId("507f1f77bcf86cd799439031"),
  name: "Bob",
  bookIds: [ObjectId("507f1f77bcf86cd799439041"), ObjectId("507f1f77bcf86cd799439043")]
}

/* Book document */
{
  _id: ObjectId("507f1f77bcf86cd799439041"),
  title: "Learn MongoDB",
  authorIds: [ObjectId("507f1f77bcf86cd799439031"), ObjectId("507f1f77bcf86cd799439032")]
}
Sample Program

This example creates two students and two courses. Each student has a list of course IDs, and each course has a list of student IDs. Then it finds all courses Alice takes by looking up her course IDs.

MongoDB
use schoolDB;

// Insert students
db.students.insertOne({ _id: ObjectId("507f1f77bcf86cd799439011"), name: "Alice", courseIds: [ObjectId("507f1f77bcf86cd799439021"), ObjectId("507f1f77bcf86cd799439022")] });
db.students.insertOne({ _id: ObjectId("507f1f77bcf86cd799439012"), name: "Bob", courseIds: [ObjectId("507f1f77bcf86cd799439022")] });

// Insert courses
db.courses.insertOne({ _id: ObjectId("507f1f77bcf86cd799439021"), title: "Math", studentIds: [ObjectId("507f1f77bcf86cd799439011"), ObjectId("507f1f77bcf86cd799439012")] });
db.courses.insertOne({ _id: ObjectId("507f1f77bcf86cd799439022"), title: "Science", studentIds: [ObjectId("507f1f77bcf86cd799439011"), ObjectId("507f1f77bcf86cd799439012")] });

// Query: Find courses Alice takes
const alice = db.students.findOne({ name: "Alice" });
const courses = db.courses.find({ _id: { $in: alice.courseIds } }).toArray();
courses;
OutputSuccess
Important Notes

Keep references consistent: if a student references a course, the course should reference that student.

Use $in operator to find documents by multiple IDs.

Summary

Many-to-many with references uses arrays of IDs in both collections.

This lets each item link to many items in the other collection.

It keeps data organized and easy to query in MongoDB.