Example 1 — Sample Data
Imagine three orders: Kit went over budget, Desk stayed under, and Lamp matched the budget exactly.
db.orders.insertMany([
{ _id: 1, item: "Kit", spent: 120, budget: 100 },
{ _id: 2, item: "Desk", spent: 80, budget: 100 },
{ _id: 3, item: "Lamp", spent: 100, budget: 100 }
]); How the Data Is Stored in MongoDB
After the insert, MongoDB keeps three documents in orders. Run find() to see them:
db.orders.find() Read each document like this:
| Field in MongoDB | Meaning |
|---|---|
item | Order name |
spent | Amount actually used |
budget | Allowed amount |
Kit: spent 120 vs budget 100 → over
Desk: spent 80 vs budget 100 → under
Lamp: spent 100 vs budget 100 → equal
$expr lets you ask: spent > budget? How It Works
- Insert once.
insertManyadds three order documents toorders. - Two number fields. Each row has
spentandbudgetso we can compare them next. - find() shows the same shape. What you inserted is what you get back—ready for
$expr.

