PerformanceExpert$lookup costjoin orderindexes for $lookup
Units sold per supplier in October 2026 (non-cancelled orders). The obvious pipeline starts at suppliers and joins products, then orders: that is one scan of ordersper product. Return supplier and units, sorted by units descending then supplier, and make the total work small: no collection scan of orders inside a $lookup.
Your answer returns 6 documents with the fields supplier, units. Order matters.
The data
The shop database from the MongoDB playground, running in your browser. "Today" is 2026-11-18.
categories18 docs
_id
ObjectId
name
string
parentId
null | ObjectId
path
string
suppliers6 docs
_id
ObjectId
name
string
country
string
rating
double
since
date
products26 docs
_id
ObjectId
sku
string
name
string
categoryId
ObjectId
supplierId
ObjectId
price
decimal
tags
array of string
stock
array of objects
warehouseqty
active
boolean
createdAt
date
customers30 docs
_id
ObjectId
name
string
email
string
tier
string
addresses
array of objects
typestreetcitycountryziplocation
preferences
object
newslettercurrency
lifetimeValue
decimal
joinedAt
date
marketingOptIn
boolean
phone
string, 24 of 30
lastLogin
date, 15 of 30
externalRef
int | string, 10 of 30
loyaltyPoints
int, 18 of 30
referredBy
ObjectId, 17 of 30
orders220 docs
_id
ObjectId
number
string
customerId
ObjectId
placedAt
date
status
string
statusHistory
array of objects
statusat
items
array of objects
productIdskunameqtyunitPrice
shipping
object
typestreetcitycountryziplocationmethod
total
decimal
currency
string
supportAgentId
ObjectId, 33 of 220
note
string, 33 of 220
reviews87 docs
_id
ObjectId
productId
ObjectId
customerId
ObjectId
orderId
ObjectId
rating
int
comment
string
at
date
employees9 docs
_id
ObjectId
name
string
title
string
reportsTo
null | ObjectId
Hints
Hint 1. Start from the side you can filter (October orders, using an index), aggregate, and only then join the few small lookups by _id.
Common traps
Joining into orders on items.productId with no index: products × orders documents read.
How this is graded here: The pipeline starts from orders with a placedAt range (index on placedAt) before any $lookup, and never $lookups into orders. (Minileaf explain does not report work done inside $lookup.)
Free for personal practice. Please don't republish the questions or solutions. See the Playground License Terms.
Run in the shell with Ctrl/⌘ + Enter. Check with Shift + Ctrl/⌘ + Enter.
"Check answer" runs your shell code on a fresh copy of the data, then on 2 hidden test datasets with more customers and orders, and compares each result with the expected one. The value of your last line is your answer.
Check your answer to see how it executes: keys and documents read, the index used, and whether it sorted in memory.