All projects
Inventory Manager
Stock tracking, suppliers, low-stock alerts, and atomic reservations.
Schema design
items
Stock levels with embedded supplier reference and reorder policy.
items document
{
_id: "SKU-1042",
name: "M4 Hex Bolt (100pk)",
onHand: 240, reserved: 15,
reorderPoint: 50, reorderQty: 500,
supplier: { id: ObjectId(), name: "FastenCo", leadDays: 7 },
location: { warehouse: "W1", aisle: "C", bin: 14 }
}movements
Append-only stock movement log (audit trail).
movements document
{ _id: ObjectId(), sku: "SKU-1042", delta: -10,
reason: "order:TX-1", at: ISODate() }Key queries
Atomic reservation (no oversell)
mongosh
db.items.updateOne(
{ _id: "SKU-1042",
$expr: { $gte: [{ $subtract: ["$onHand", "$reserved"] }, 10] } },
{ $inc: { reserved: 10 } }
)Low-stock report
mongosh
db.items.find({ $expr: { $lte: ["$onHand", "$reorderPoint"] } })Indexes
db.items.createIndex({ "supplier.id": 1 })List everything from one supplier when placing purchase orders.
db.movements.createIndex({ sku: 1, at: -1 })Movement history per item.
Aggregations
Inventory value per warehouse
mongosh
db.items.aggregate([
{ $group: { _id: "$location.warehouse",
totalValue: { $sum: { $multiply: ["$onHand", "$unitCost"] } } } }
])Performance tips
- 1.The filtered atomic update is the whole trick: check-and-decrement in one operation means no race conditions and no transactions needed.
- 2.Movements are append-only; onHand is a rebuildable cache.
Want to experiment with these patterns? Open the playground and build the collections yourself, or practice related skills in the challenges.