How do you model a many-to-many relationship in MongoDB with an example

0 votes
Can you tell me How do you model a many-to-many relationship in MongoDB with an example?
Feb 22, 2025 in Node-js by Ashutosh
• 33,350 points
558 views

1 answer to this question.

0 votes

In MongoDB, a many-to-many relationship can be modeled in a couple of ways. Here are the most common approaches:

1. Using Reference Documents

In this approach, you create separate collections for each of the entities and store references (IDs) to related documents.

Example: Students and Courses

Students Collection:

{

  "_id": ObjectId("student1"),

  "name": "Alice",

  "courseIds": [ObjectId("course1"), ObjectId("course2")]

}

{

  "_id": ObjectId("student2"),

  "name": "Bob",

  "courseIds": [ObjectId("course2"), ObjectId("course3")]

}


Courses Collection:

{

  "_id": ObjectId("course1"),

  "title": "Math 101",

  "studentIds": [ObjectId("student1")]

}

{

  "_id": ObjectId("course2"),

  "title": "History 201",

  "studentIds": [ObjectId("student1"), ObjectId("student2")]

}

{

  "_id": ObjectId("course3"),

  "title": "Science 301",

  "studentIds": [ObjectId("student2")]

}
answered Feb 23, 2025 by Kavya

Related Questions In Node-js

0 votes
1 answer

How would you model a one-to-many relationship in MongoDB?

In MongoDB, a one-to-many relationship can be ...READ MORE

answered Feb 22, 2025 in Node-js by Kavya
545 views
0 votes
1 answer

How do you design a schema for tree structures in MongoDB?

Designing a schema for tree structures in ...READ MORE

answered Feb 22, 2025 in Node-js by Kavya
549 views
0 votes
1 answer

How do you embed a document in MongoDB for better performance?

Embedding documents in MongoDB is a common ...READ MORE

answered Feb 22, 2025 in Node-js by Kavya
490 views
0 votes
1 answer

How do you redirect a user to a different route in React Router?

Redirecting a User in React Router 1. Using ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
498 views
0 votes
1 answer

What is the difference between RDBMS relationships and MongoDB’s data model?

Feature RDBMS (SQL Databases) MongoDB (NoSQL Document Database) Data Structure Tables ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
481 views
0 votes
1 answer

Write a query for a compound index to optimize a search operation in MongoDB.

A compound index improves search performance by ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
541 views
0 votes
1 answer

How do you handle concerns for write operations in MongoDB?

Write operations in MongoDB need to be ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
546 views
0 votes
1 answer

Write a query to fetch specific fields using MongoDB projections.

Fetching Specific Fields Using MongoDB Projections MongoDB projections ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
540 views
0 votes
1 answer
0 votes
1 answer

What are MongoDB data types, and how do you define them in a schema?

MongoDB supports various data types, including: String (String) ...READ MORE

answered Feb 23, 2025 in Node-js by anonymous
516 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP