Showing posts with label MongoDB Challenge. Show all posts
Showing posts with label MongoDB Challenge. Show all posts

Extra Challenge: Using References Between Documents

 

๐ŸŽฏ ๐Ÿ’ก Extra Challenge: Using References Between Documents



Here's an Extra Challenge designed to build on the original MongoDB modeling task. This one introduces referencing between collections, helping learners understand when to use references instead of embedding — a key design concept in NoSQL databases like MongoDB.


๐Ÿ“Œ Scenario

Instead of embedding full course details in each student document (which can lead to duplication), you'll now reference courses using course_id.


๐Ÿ”น Step 1: Modify the Student Document

Update your students collection to reference enrolled courses like this:

{
  "student_id": 102,
  "name": "Ravi Kumar",
  "class": "10B",
  "date_of_birth": "2008-07-20T00:00:00Z",
  "contact_info": {
    "phone": "9876504321",
    "email": "ravi.kumar@example.com"
  },
  "course_ids": [301, 302],  // References to course documents
  "marks": [
    { "subject": "Math", "score": 91 },
    { "subject": "Science", "score": 86 }
  ]
}

๐Ÿ”น Step 2: Sample Courses (Already Inserted)

{
  "course_id": 301,
  "title": "Mathematics",
  "teacher_name": "Mr. Arjun Mehta",
  "schedule": ["Mon", "Wed", "Fri"]
}
{
  "course_id": 302,
  "title": "Science",
  "teacher_name": "Ms. Reema Das",
  "schedule": ["Tue", "Thu"]
}

๐Ÿ” Challenge Task

  1. Store course references in the students collection using course_ids

  2. Write a MongoDB query to retrieve a student’s name and the full details of the courses they are enrolled in


๐Ÿง  Hint for the Query

You’ll need to:

  • Use findOne() or aggregate() to get the student

  • Extract the course_ids

  • Use $lookup or multiple find() queries to fetch related course details from the courses collection

๐Ÿ”ง This mimics a one-to-many relationship without embedding full course documents.


๐Ÿง  Why This Matters

  • Demonstrates when and why referencing is better than embedding

  • Teaches the concept of manual joins in NoSQL

  • Prepares learners for more advanced aggregation and relationship modeling in MongoDB



  •  solution using $lookup (MongoDB aggregation): coming next


Solution MongoDB document Modelling Assignment

 Here's the complete solution to your MongoDB document modeling assignment, including:

  • ✅ Sample documents for students and courses collections

  • ✅ Correct JSON structure

  • ✅ MongoDB queries for the bonus task

  • ✅ Notes on best practices (embed vs reference)


๐Ÿงพ ✅ MongoDB Document Modeling – Solution


๐Ÿ”น 1. Sample Document for students Collection

{
  "student_id": 101,
  "name": "Aisha Khan",
  "class": "10A",
  "date_of_birth": "2008-05-10T00:00:00Z",
  "contact_info": {
    "phone": "9876543210",
    "email": "aisha.khan@example.com"
  },
  "marks": [
    { "subject": "Math", "score": 85 },
    { "subject": "Science", "score": 90 },
    { "subject": "English", "score": 88 }
  ]
}

๐Ÿ”น 2. Sample Document for courses Collection

{
  "course_id": 301,
  "title": "Mathematics",
  "teacher_name": "Mr. Arjun Mehta",
  "schedule": ["Mon", "Wed", "Fri"]
}

You can insert these into MongoDB using:

db.students.insertOne({ /* student document above */ });
db.courses.insertOne({ /* course document above */ });

๐Ÿ”น 3. MongoDB Query – Bonus Task

Q: Find all students who scored above 80 in Math and display only their name and Math score.

✅ MongoDB Query:

db.students.find(
  { "marks": { $elemMatch: { "subject": "Math", "score": { $gt: 80 } } } },
  {
    "name": 1,
    "marks": {
      $filter: {
        input: "$marks",
        as: "mark",
        cond: { $and: [ { $eq: ["$$mark.subject", "Math"] } ] }
      }
    }
  }
);

๐Ÿง  Explanation:

  • $elemMatch filters students with at least one "Math" score > 80.

  • $filter inside the projection limits returned marks to only the Math subject.


๐Ÿ“ Notes & Best Practices

  • Embedding marks inside the student document is useful when the marks are tightly coupled with the student (1:1 ownership).

  • Use referencing (e.g. linking course_id) when many students share the same course and you need cross-document querying or analytics.

  • Store date_of_birth in ISO 8601 format for date operations.


๐Ÿง  Summary

You’ve now:

  • Designed realistic NoSQL documents

  • Learned how to embed nested data

  • Queried and filtered embedded arrays


Next: Extra Practice Challenge



Featured Post

Extra Challenge: Using References Between Documents

  ๐ŸŽฏ ๐Ÿ’ก Extra Challenge: Using References Between Documents Here's an  Extra Challenge  designed to build on the original MongoDB mod...

Popular Posts