The Brief

You're building the backend for FreshBasket, a grocery delivery platform. Your mission: create a production-ready product catalog with:

  • CRUD operations — Create, read, update, and delete products.
  • Full-text search — Let customers find "avocado" or "organic honey" instantly.
  • Smart filtering — By category, price range, ratings, and stock levels.
  • Analytics pipelines — Revenue by category, top suppliers, low-stock alerts, and inventory value metrics.
  • Batch operations — Seed 100+ products and adjust prices at scale.

By the end, you'll have a fully functional MongoDB backend that powers a real grocery app. Let's build it.

Setup & Indexes

Before writing code, you need to connect to MongoDB and create indexes for performance. Text indexes enable full-text search; other indexes speed up common queries.

Q0: Connect and Create Indexes

python
Indexes created. Catalog ready for operations.

What these indexes do:

  • create_index([('name', 'text'), ...]) — Text index for full-text search across name, category, and tags.
  • create_index([('category', 1)]) — Speed up category filters.
  • create_index([('supplier.name', 1)]) — Speed up supplier lookups.
  • create_index([('avg_rating', -1)]) — Speed up sorting by rating (descending).
  • create_index([('stock', 1)]) — Speed up stock availability checks.

CRUD Operations

Q1: Create Product

python
Created product: 69e0a6a14d75051788b18efa

Q2: Get Product by ID

python
Product: Test Organic Honey 500g
Stock: 75
Rating: 4.6 (0 reviews)

Q3: Update Stock (After Purchase)

python
Sold 5 units. New stock: 70

Q4: Add Tag (Without Duplicates)

python
Tags updated

Q5: Add Review & Update Rating

python
New avg: 5.0, Count: 1

Q6: Delete Product

python
Deleted 1 product(s)

Search & Filter

Q7: Full-Text Search

python
Organic Avocados (6-pack) - KES 1341

Q8: Filter by Category

python
Found 10 fruit products

Q9: Affordable Products (Price Range)

python
Found 23 items under 500 KES

Q10: Top Rated Products

python
Fairy Dish Soap 750ml - 5.0⭐
Cabbage 1pc - 4.9⭐
Cheddar Cheese 200g - 4.9⭐

Analytics Pipelines

MongoDB aggregation pipelines let you analyze data at scale. Use $addFields, $group, and $sort to unlock insights.

Q11: Revenue by Category (Inventory Value)

python
Spices: KES 2487802 (10 products)
Grains: KES 2311165 (10 products)
Dairy: KES 2094267 (10 products)

Q12: Top Suppliers (by Inventory Value)

python
Deccan Organics: KES 2687384 (Bangalore)
Coast Fisheries: KES 2007969 (Mombasa)
Thika Fresh: KES 1867087 (Thika)

Q13: Recently Added Products

python
Found 10 recently added products

Q14: Low Stock Alert

python
WARNING: 3 products under 10 units
Spaghetti 500g: 8 units
Potato Crisps 200g: 8 units
Croissants 4-pack: 8 units

Q15: Category Summary (Overview)

python
Dairy: 10 products, KES 294-2214, Rating: 4.08
Vegetables: 10 products, KES 163-2307, Rating: 4.14
Spices: 10 products, KES 525-2383, Rating: 4.08

Batch Operations

Q16: Seed Catalog (Bulk Insert)

python
Seeded 5 products

Q17: Bulk Price Adjustment

python
Updated 5 products with 10% price increase

Running the Demo

Here's a complete main function that runs all operations together:

python
=== FreshBasket Product Catalog ===

1. Setting up indexes...
2. Creating a new product...
Created: 69e0a6a14d75051788b18efa
[... full output continues ...]

Challenge

Add an endpoint that returns products sorted by inventory value (price × stock) with pagination.

Requirements:

  • Calculate inventory value = price × stock for each product.
  • Sort by inventory value descending (highest value first).
  • Support pagination with page and limit parameters.
  • Return total count, current page, and products.
Show hint

Use an aggregation pipeline with $addFields to compute inventory_value, then $sort, $skip, and $limit for pagination:

python