MongoDB Document Modeling – Practice Assignment

 

๐Ÿงพ MongoDB Document Modeling – Practice Assignment

Title: Design a NoSQL Document Structure for a School Management System

Level: Beginner to Intermediate
Objective: Learn how to organize and model data using MongoDB’s document structure.


๐Ÿ“Œ Assignment Brief

You are designing a database for a School Management System using MongoDB. Your task is to model documents for students, courses, and marks using a proper JSON-like structure.

You need to:


๐Ÿ”น 1. Create a students collection

Each student document should include:

  • student_id (Number)

  • name (String)

  • class (String)

  • date_of_birth (ISO Date format)

  • contact_info (Nested object with phone and email)


๐Ÿ”น 2. Create a courses collection

Each course document should include:

  • course_id (Number)

  • title (String)

  • teacher_name (String)

  • schedule (Array of days, e.g., ["Mon", "Wed", "Fri"])


๐Ÿ”น 3. Embed marks within each student document

Instead of a separate marks collection, embed a list of subjects and scores within the student document like this:

"marks": [
  { "subject": "Math", "score": 85 },
  { "subject": "Science", "score": 90 }
]

๐Ÿง  Optional Bonus Task

Write a MongoDB query to:

  • Find students who scored above 80 in Math

  • Project only the student's name and Math score


๐Ÿงช Expected Sample Document (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 }
  ]
}

๐Ÿง  Why This Matters

  • Reinforces document structure and nesting in MongoDB

  • Helps students understand when to embed vs reference

  • Mirrors real-world use cases like student performance tracking


solution :


CRUD Operations in SQL vs NoSQL Explained with Simple Examples

๐Ÿ”ท Part 6: CRUD Operations in SQL vs NoSQL – A Beginner's Guide


This post will:

  • ✅ Compare how to CreateReadUpdate, and Delete data in both systems

  • ✅ Use simple, realistic examples

  • ✅ Help beginners understand key syntax differences


๐Ÿ“ Introduction

In any application or system that works with data, you need to perform four basic operations:

  • Create new data

  • Read existing data

  • Update current data

  • Delete data you no longer need

These are called CRUD operations — and whether you're using SQL or NoSQL, they form the core of working with databases.

Let’s explore these operations in both SQL (like MySQL/PostgreSQL) and NoSQL (like MongoDB) with clear, side-by-side examples.


๐Ÿ”ธ Assumed Data Structure

We’ll use a simple students table/collection with:

  • StudentID

  • Name

  • Class

  • Marks (embedded in NoSQL)


๐Ÿ“Œ 1. CREATE – Inserting Data

✅ SQL (MySQL/PostgreSQL)

INSERT INTO Students (StudentID, Name, Class)
VALUES (1, 'Aisha', '10A');

✅ NoSQL (MongoDB)

db.students.insertOne({
  student_id: 1,
  name: "Aisha",
  class: "10A",
  marks: [
    { subject: "Math", score: 85 }
  ]
});

๐Ÿ“Œ 2. READ – Retrieving Data

✅ SQL

SELECT * FROM Students WHERE StudentID = 1;

✅ NoSQL

db.students.findOne({ student_id: 1 });

๐Ÿ“Œ 3. UPDATE – Changing Data

✅ SQL

UPDATE Students
SET Class = '10B'
WHERE StudentID = 1;

✅ NoSQL

db.students.updateOne(
  { student_id: 1 },
  { $set: { class: "10B" } }
);

๐Ÿ“Œ 4. DELETE – Removing Data

✅ SQL

DELETE FROM Students
WHERE StudentID = 1;

✅ NoSQL

db.students.deleteOne({ student_id: 1 });

๐Ÿ“Š Quick Comparison Table

Operation SQL Syntax MongoDB (NoSQL) Syntax
Create INSERT INTO ... VALUES (...) insertOne({ ... })
Read SELECT * FROM ... WHERE ... findOne({ ... })
Update UPDATE ... SET ... WHERE ... updateOne({ ... }, { $set: { ... } })
Delete DELETE FROM ... WHERE ... deleteOne({ ... })

๐Ÿง  Summary

SQL NoSQL (MongoDB)
Structured (tables/rows) Flexible (documents/JSON)
Uses SQL language Uses JavaScript-like syntax
Schema-based Schema-less
Great for complex queries Great for rapid, dynamic data

✅ What’s Next?

In Part 7, we’ll explore Normalization vs Denormalization — how SQL and NoSQL structure data differently for performance and flexibility.


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 mode...

Popular Posts