Assignements Of Day 12: Data Modeling in MongoDB
Assignment 1: Design a Library System Schema (Books, Authors, Borrowers)
Task: Design a MongoDB schema for a library system with collections for books, authors, and borrowers. Embed and reference data appropriately.
Solution:
1. Authors Collection:
o Store author details, including name, dob, and bio.
o This data will be stored in a separate collection and referenced in the books collection.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a122"),
"name": "J.K. Rowling",
"dob": "1965-07-31",
"bio": "British author, best known for the Harry Potter series."
}
2. Books Collection:
o Store title, author (reference to the author), and a list of borrowers who have borrowed the book.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a123"),
"title": "Harry Potter and the Philosopher's Stone",
"author": ObjectId("60d5f7e2bcf86f431bc8a122"), // Reference to authors collection
"genre": "Fantasy",
"publication_year": 1997,
"borrowers": [
{ "borrower_id": ObjectId("60d5f7e2bcf86f431bc8a124"), "borrowed_on": "2025-01-01", "due_date": "2025-01-15" }
]
}
3. Borrowers Collection:
o Store borrower information and reference books they have borrowed.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a124"),
"name": "Alice",
"email": "alice@example.com",
"borrowed_books": [
ObjectId("60d5f7e2bcf86f431bc8a123") // Reference to books collection
]
}
Assignment 2: Design an E-Commerce System Schema (Products, Categories, Orders, Users)
Task: Model an e-commerce system with collections for products, categories, orders, and users. Use a mix of embedded and referenced models.
Solution:
1. Categories Collection:
o Store name and description of product categories.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a130"),
"name": "Electronics",
"description": "Devices and gadgets"
}
2. Products Collection:
o Reference the category and embed reviews as an array.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a131"),
"name": "Smartphone",
"category": ObjectId("60d5f7e2bcf86f431bc8a130"), // Reference to categories collection
"price": 699,
"description": "Latest model with high-end features.",
"reviews": [
{ "user": "Alice", "rating": 4, "comment": "Great phone!" },
{ "user": "Bob", "rating": 5, "comment": "Excellent value for money!" }
]
}
3. Orders Collection:
o Store user (referenced), products (embedded with quantity), and status.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a132"),
"user": ObjectId("60d5f7e2bcf86f431bc8a135"), // Reference to users collection
"products": [
{ "product_id": ObjectId("60d5f7e2bcf86f431bc8a131"), "quantity": 1 }
],
"order_date": "2025-01-15",
"status": "Shipped"
}
4. Users Collection:
o Store user information and reference their orders.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a135"),
"name": "Alice",
"email": "alice@example.com",
"password": "hashed_password",
"orders": [
ObjectId("60d5f7e2bcf86f431bc8a132") // Reference to orders collection
]
}
Assignment 3: Design a Restaurant Reservation System (Customers, Tables, Reservations, Menu)
Task: Design a data model for a restaurant reservation system. Use appropriate embedded and referenced models.
Solution:
1. Customers Collection:
o Store customer details.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a140"),
"name": "John Doe",
"email": "johndoe@example.com",
"phone_number": "123-456-7890"
}
2. Tables Collection:
o Store table details and seating capacity.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a141"),
"table_number": 1,
"seating_capacity": 4
}
3. Menu Collection:
o Embed menu items, including name, category, and ingredients.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a142"),
"name": "Spaghetti",
"category": "Main Course",
"price": 15.99,
"ingredients": ["Pasta", "Tomato sauce", "Olive oil", "Cheese"]
}
4. Reservations Collection:
o Embed details for the customer, table, and reservation time.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a143"),
"customer": ObjectId("60d5f7e2bcf86f431bc8a140"), // Reference to customers collection
"table": ObjectId("60d5f7e2bcf86f431bc8a141"), // Reference to tables collection
"reservation_time": "2025-02-01T18:00:00",
"status": "Confirmed"
}
Assignment 4: Design a Schema for a Blog System (Posts, Authors, Comments)
Task: Model a blogging platform with posts, authors, and comments. Use appropriate embedding and referencing.
Solution:
1. Authors Collection:
o Store author details.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a150"),
"name": "Alice",
"email": "alice@example.com"
}
2. Posts Collection:
o Store title, content, and reference author. Embed comments.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a151"),
"title": "Introduction to MongoDB",
"author": ObjectId("60d5f7e2bcf86f431bc8a150"), // Reference to authors collection
"content": "This is a blog post about MongoDB.",
"comments": [
{ "user": "Bob", "comment": "Great post!", "date": "2025-01-28" }
]
}
Assignment 5: Design a Social Media System (Users, Posts, Likes, Comments)
Task: Model a social media system where users can post content, like posts, and comment on posts.
Solution:
1. Users Collection:
o Store user details.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a160"),
"name": "Bob",
"email": "bob@example.com"
}
2. Posts Collection:
o Store post content and embed likes (array of users) and comments (array of comment objects).
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a161"),
"user": ObjectId("60d5f7e2bcf86f431bc8a160"), // Reference to users collection
"content": "This is my first post!",
"likes": [ObjectId("60d5f7e2bcf86f431bc8a160")], // List of user IDs who liked the post
"comments": [
{ "user": "Alice", "comment": "Nice post!", "date": "2025-01-28" }
]
}
Assignment 6: Design a Course Management System (Courses, Students, Enrollments)
Task: Create a schema for a course management system with courses, students, and enrollments.
Solution:
1. Courses Collection:
o Store course_name, instructor, and duration.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a170"),
"course_name": "Introduction to Python",
"instructor": "Dr. Smith",
"duration": "3 months"
}
2. Students Collection:
o Store student details.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a171"),
"name": "Alice",
"email": "alice@example.com"
}
3. Enrollments Collection:
o Reference students and courses collections.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a172"),
"student": ObjectId("60d5f7e2bcf86f431bc8a171"), // Reference to student
"course": ObjectId("60d5f7e2bcf86f431bc8a170"), // Reference to course
"enrollment_date": "2025-01-01"
}
Assignment 7: Design a Task Management System (Tasks, Users, Comments)
Task: Design a task management system where users can create tasks, add comments, and mark tasks as completed.
Solution:
1. Users Collection:
o Store name, email, and other user details.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a180"),
"name": "John",
"email": "john@example.com"
}
2. Tasks Collection:
o Store title, assigned_to (user reference), and status (e.g., "pending", "completed").
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a181"),
"title": "Complete MongoDB assignment",
"assigned_to": ObjectId("60d5f7e2bcf86f431bc8a180"), // Reference to users collection
"status": "pending",
"comments": [
{ "user": "John", "comment": "Started working on it.", "date": "2025-01-28" }
]
}
Assignment 8: Design a Movie Rental System (Movies, Customers, Rentals)
Task: Design a movie rental system where customers can rent movies and track rental periods.
Solution:
1. Movies Collection:
o Store title, genre, and price.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a190"),
"title": "Inception",
"genre": "Sci-Fi",
"price": 4.99
}
2. Customers Collection:
o Store customer information.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a191"),
"name": "Michael",
"email": "michael@example.com"
}
3. Rentals Collection:
o Store movie rental details, including the customer and rental period.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a192"),
"customer": ObjectId("60d5f7e2bcf86f431bc8a191"), // Reference to customer collection
"movie": ObjectId("60d5f7e2bcf86f431bc8a190"), // Reference to movies collection
"rental_start_date": "2025-01-25",
"rental_end_date": "2025-01-30"
}
Assignment 9: Design an Inventory System (Products, Suppliers, Stock)
Task: Design a schema for an inventory management system with products, suppliers, and stock.
Solution:
1. Suppliers Collection:
o Store supplier details.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a200"),
"name": "XYZ Suppliers",
"contact": "123-456-7890"
}
2. Products Collection:
o Store product_name, supplier, and price.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a201"),
"product_name": "Laptop",
"supplier": ObjectId("60d5f7e2bcf86f431bc8a200"), // Reference to suppliers collection
"price": 999.99
}
3. Stock Collection:
o Store product reference and the number of units in stock.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a202"),
"product": ObjectId("60d5f7e2bcf86f431bc8a201"), // Reference to products collection
"quantity_in_stock": 50
}
Assignment 10: Design a Job Portal System (Jobs, Candidates, Applications)
Task: Design a job portal schema with jobs, candidates, and applications.
Solution:
1. Jobs Collection:
o Store title, company_name, and requirements.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a210"),
"title": "Software Engineer",
"company_name": "Tech Corp",
"requirements": ["Python", "Django", "SQL"]
}
2. Candidates Collection:
o Store candidate details.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a211"),
"name": "Alice",
"email": "alice@example.com",
"skills": ["Python", "Django"]
}
3. Applications Collection:
o Store references to the job and candidate, and the application status.
json
CopyEdit
{
"_id": ObjectId("60d5f7e2bcf86f431bc8a212"),
"candidate": ObjectId("60d5f7e2bcf86f431bc8a211"), // Reference to candidates collection
"job": ObjectId("60d5f7e2bcf86f431bc8a210"), // Reference to jobs collection
"status": "Under Review"
}
These assignments are designed to help students practice their MongoDB data modeling skills with different real-world use cases. Each solution demonstrates how to combine embedding and referencing based on specific use cases for better performance and data organization.
