2. Build a Consistent Marketplace Dataset

Dataset used throughout this chapter

The examples use orders, products, and customers. Each order stores a customer reference and an array of purchased items. Each item records its product identifier, quantity, and historical unit price. Historical prices matter because a product's current catalog price may differ from the price originally paid.

The sample data includes several statuses so that filters and business rules can be practiced. Run this in mongosh. If these identifiers already exist, clear the lab collections or choose different identifiers.

Define revenue carefully

Decide whether cancelled orders, shipping fees, discounts, taxes, and refunds are included. Different business rules produce different valid totals.

Code Example
db.orders.insertMany([
  { _id: 1001, customerId: 501, status: "delivered", createdAt: ISODate("2026-10-01T10:00:00Z"), discount: 100, shippingFee: 50, items: [{ productId: 101, name: "Mechanical Keyboard", category: "Electronics", quantity: 2, price: 2500 }, { productId: 102, name: "USB-C Cable", category: "Accessories", quantity: 3, price: 300 }] },
  { _id: 1002, customerId: 502, status: "shipped", createdAt: ISODate("2026-10-02T12:30:00Z"), discount: 0, shippingFee: 80, items: [{ productId: 103, name: "Monitor", category: "Electronics", quantity: 1, price: 12000 }] },
  { _id: 1003, customerId: 501, status: "delivered", createdAt: ISODate("2026-10-03T09:15:00Z"), discount: 200, shippingFee: 50, items: [{ productId: 104, name: "Office Chair", category: "Furniture", quantity: 1, price: 8000 }, { productId: 102, name: "USB-C Cable", category: "Accessories", quantity: 2, price: 280 }] },
  { _id: 1004, customerId: 503, status: "cancelled", createdAt: ISODate("2026-10-04T14:00:00Z"), discount: 0, shippingFee: 0, items: [{ productId: 101, name: "Mechanical Keyboard", category: "Electronics", quantity: 1, price: 2500 }] },
  { _id: 1005, customerId: 502, status: "delivered", createdAt: ISODate("2026-10-05T08:45:00Z"), discount: 150, shippingFee: 60, items: [{ productId: 105, name: "Desk Lamp", category: "Furniture", quantity: 2, price: 1500 }, { productId: 103, name: "Monitor", category: "Electronics", quantity: 1, price: 11500 }] }
]);

db.products.insertMany([
  { _id: 101, name: "Mechanical Keyboard", category: "Electronics", currentPrice: 2700, stock: 30 },
  { _id: 102, name: "USB-C Cable", category: "Accessories", currentPrice: 350, stock: 200 },
  { _id: 103, name: "Monitor", category: "Electronics", currentPrice: 12500, stock: 15 },
  { _id: 104, name: "Office Chair", category: "Furniture", currentPrice: 8500, stock: 8 },
  { _id: 105, name: "Desk Lamp", category: "Furniture", currentPrice: 1600, stock: 45 }
]);

db.customers.insertMany([
  { _id: 501, name: "Ananya", city: "Chennai", tier: "gold" },
  { _id: 502, name: "Rahul", city: "Bengaluru", tier: "silver" },
  { _id: 503, name: "Priya", city: "Hyderabad", tier: "bronze" }
]);
Key Takeaways
  • Order items preserve historical prices.
  • Customer and product identifiers connect collections.
  • Use BSON Date values for date analytics.
  • Define business rules before calculating revenue.
Knowledge Check

Why preserve the price charged at purchase time in an order?