querydrill

Learn MongoDB by running queries, not reading about them

54 lessons and 38 hand-written exercises. Get one wrong and it tells you which field was wrong — not just that you were.

No signup. No install. No database. Every query runs in this tab.

Top three products by revenue, across 200 orders. Press Run — nothing is sent anywhere, the data is already in your browser. Press Edit and it becomes a real editor.

db.orders.aggregate([
  { $unwind: "$items" },
  { $group: {
      _id: "$items.product",
      revenue: { $sum: { $multiply: ["$items.price", "$items.quantity"] } }
  } },
  { $sort: { revenue: -1 } },
  { $limit: 3 }
])

Wrong answers get a reason

A common mistake on one of the 38 drills: summing the unit price and forgetting the quantity. Here is what the site says about it.

db.orders.aggregate([
  { $match: { status: "completed" } },
  { $unwind: "$items" },
  { $group: {
      _id: "$items.product",
      revenue: { $sum: "$items.price" }
  } },
  { $sort: { revenue: -1 } }
])
Not quite:
  • row 0.revenue: expected 53000, got 27000
  • row 1.revenue: expected 31800, got 17400
  • row 2.revenue: expected 22200, got 12000
and 8 more differences
What usually goes wrong here
  • $sum: "$items.price" adds one unit price per line and ignores quantity. Every number is too low and the ranking can still look reasonable.
  • $group before the $unwind groups whole orders, so "$items.product" is an array of products rather than one product.

That feedback was produced by the grader while this page was being built, not written out by hand. Try the drill.

What is different about it

The usual wayHere
When you are wrong“Incorrect.”The field, what was expected, and what you returned.
Expected answersSaved output, which drifts when the data changes.A reference query, run against the same data as yours, every time.
Before you startAn account, and often a database to connect.Nothing. The page is the database.
Exercise countPadded — the same question with a different product name.38 drills, each a different concept.

How it works

Your browser is the database

Queries run through mingo, a JavaScript implementation of the MongoDB query language. There is no server to send a query to, so nothing you type leaves the tab — and the site works offline.

The data is the same for everyone

30 users, 200 orders and 11 products, built in your tab from a fixed seed. Write queries change it; a bar appears offering to put it back.

It is open source

MIT licensed, and an exercise is a small contribution to make — one object in one file, with tests that say what you missed.

The course

54 lessons · 38 drills

MongoDB fundamentals

6 modules

Querying and updating documents: find, operators, nested data, arrays, sorting and writes.

Aggregation

3 modules

The pipeline: filtering, reshaping, grouping, and flattening arrays with $unwind.

Advanced aggregation

3 modules

Joins, array expression operators, conditionals, $facet, dates, and full interview problems.

Coming soon, through a local npx mode: connect your own MongoDB,explain(), and index tuning.