Practice and reinforce the concepts from Lesson 13
You've just been hired as a Database Developer at MegaMart! Your manager wants you to create a new department in their flexible product catalog. The twist? You get to choose what type of products to sell, and MongoDB's flexibility means you can store ANY product attributes you need!
Your challenge: Design and implement a complete product department with at least 5 unique products, build smart queries to analyze your inventory, and create a recommendation system.
A fully functional product department that includes:
Choose your department (or create your own):
// Replace 'your_department' with your chosen department name
use megamart_catalog
// Document your department choice
db.departments.insertOne({
name: "your_department",
manager: "Your Name",
description: "What makes your department special?",
created: new Date()
})
Think about what attributes your products need. Here's a template:
// Example: Fitness Equipment Department
/*
Product attributes to consider:
- name, price, category
- Special attributes for your department
- Nested objects for complex data
- Arrays for multiple values
- Reviews and ratings
*/
// Design at least 5 different products with varying attributes
// Product 1: _____
// Product 2: _____
// Product 3: _____
// Product 4: _____
// Product 5: _____
// Create your products collection
db.products.insertMany([
{
// Product 1
department: "your_department",
name: "",
price: 0,
// Add your unique attributes here
},
{
// Product 2
department: "your_department",
name: "",
price: 0,
// Different attributes? No problem!
},
// Add at least 3 more products
])
// Verify your products
db.products.find({ department: "your_department" }).pretty()
Add at least one product with:
// Example: Product with rich features
db.products.insertOne({
department: "your_department",
name: "Premium Product Name",
price: 0,
// Add nested structure here
specifications: {
// Your specs
},
// Add array data here
features: [],
// Add reviews
reviews: [
{
user: "",
rating: 5,
comment: "",
helpful: 0
}
]
})
// Find all products in your department under a certain price
// Your query:
// Find the most expensive product in your department
// Your query:
// Calculate average price for your department
// Your query:
// Search for products with specific features
// (Use attributes unique to your department)
// Your query:
// Find products with missing optional attributes
// Your query:
// Complex query with multiple conditions
// Your query:
// Find your highest-rated products
// Your query:
// Count products by category within your department
// Your query:
Build an aggregation pipeline to analyze your department:
// Pipeline 1: Department Overview
db.products.aggregate([
// Match your department
// Group by category
// Calculate stats
// Sort results
])
// Pipeline 2: Popular Features
// What features/attributes appear most in your products?
db.products.aggregate([
// Your pipeline here
])
// Pipeline 3: Price Distribution
// Analyze price ranges in your department
db.products.aggregate([
// Your pipeline here
])
// Given a product, find similar products
function findSimilarProducts(productName) {
// Step 1: Get the original product
// Step 2: Find products with:
// - Same category
// - Similar price (within 30%)
// - At least one matching attribute
// Your implementation:
}
// Test your recommendation system
findSimilarProducts("Your Product Name")
// Add stock tracking that works for your product type
// Consider: physical inventory, digital licenses, availability dates
// Implement a discount system without changing product structure
// Use a separate collection for active promotions
// Create indexes to speed up your most common queries
// Implement text search across product descriptions
Schema Design: How did MongoDB's flexibility help you store diverse products? What would this look like in SQL?
Query Patterns: Which queries were easier/harder to write? Why?
Real-World Application: How could your department structure scale to thousands of products?
Ready to expand MegaMart? Your flexible MongoDB design can handle anything! 🎉