Get Started Free

1. The overstock list Solved

CoreMedium$sum on arrays$project$match after $project

Merchandising wants to discount products that are sitting on too much stock. Stock for each product is split across warehouses in a stock array. Return every active product holding more than 160 units in total across all warehouses, with sku, name, totalStock and the number of warehouses that carry it. Sort by totalStock descending, then sku. (Discontinued products are cleared out separately.)

Your answer returns 8 documents with the fields name, sku, totalStock, warehouses. Order matters.

The data

The shop database from the MongoDB playground, running in your browser. "Today" is 2026-11-18.

categories18 docs
_idObjectId
namestring
parentIdnull | ObjectId
pathstring
suppliers6 docs
_idObjectId
namestring
countrystring
ratingdouble
sincedate
products26 docs
_idObjectId
skustring
namestring
categoryIdObjectId
supplierIdObjectId
pricedecimal
tagsarray of string
stockarray of objects
warehouse qty
activeboolean
createdAtdate
customers30 docs
_idObjectId
namestring
emailstring
tierstring
addressesarray of objects
type street city country zip location
preferencesobject
newsletter currency
lifetimeValuedecimal
joinedAtdate
marketingOptInboolean
phonestring, 24 of 30
lastLogindate, 15 of 30
externalRefint | string, 10 of 30
loyaltyPointsint, 18 of 30
referredByObjectId, 17 of 30
orders220 docs
_idObjectId
numberstring
customerIdObjectId
placedAtdate
statusstring
statusHistoryarray of objects
status at
itemsarray of objects
productId sku name qty unitPrice
shippingobject
type street city country zip location method
totaldecimal
currencystring
supportAgentIdObjectId, 33 of 220
notestring, 33 of 220
reviews87 docs
_idObjectId
productIdObjectId
customerIdObjectId
orderIdObjectId
ratingint
commentstring
atdate
employees9 docs
_idObjectId
namestring
titlestring
reportsTonull | ObjectId

Hints

Common traps
  • Forgetting active: true: one discontinued product is heavily overstocked and sneaks into the list.
  • Ties: two products hold exactly 172 units, so the sku tie-breaker decides their order.

Solution

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.

Starting…