Developer and Text Tools

How to Learn MongoDB Queries and Aggregation Pipelines Step by Step

Practise MongoDB find queries, operators, updates and aggregation pipelines on your own JSON, and debug pipelines by inspecting the output of each stage.

Saizul Amin
Saizul Amin2 Nov 2026 · 3 min read
How to Learn MongoDB Queries and Aggregation Pipelines Step by Step

How to Learn MongoDB Queries and Aggregation Pipelines Step by Step

MongoDB queries look simple at first: a filter document in curly braces. Then come array matching, the difference between $elemMatch and dot paths, and aggregation pipelines with ten stages where one wrong field name returns nothing at all. The best way to learn and to debug is to experiment on small data and look at every intermediate result. This guide shows how, with the free MongoDB query playground, which runs a MongoDB-compatible engine in your browser.

Start with real-looking data

Copy a few dozen documents from your database with mongoexport, Compass or Atlas, or write them by hand. Extended JSON with $oid and $date converts automatically, and shell literals such as ObjectId() and ISODate() are understood. Put related data into separate collections, for example orders and customers, so you can practise joins. The schema view lists every field path and its types, which quickly reveals documents where a number was stored as a string.

Filters and how arrays behave

A filter matches documents whose fields satisfy every condition. Comparison operators such as $gt and $lte, set operators $in and $nin, and $exists and $type cover most needs. Arrays are where surprises happen. A condition on an array field matches when any element matches, so tags: "gift" finds documents whose tags array contains gift. Two conditions on a field inside an array of objects may be satisfied by different elements; $elemMatch forces them onto the same element. Try both versions on the sample data and compare the counts.

Projection, sorting and paging

The second argument of find chooses fields. Include fields with 1 or exclude them with 0, but do not mix the two except for _id. Sort, skip and limit work as in the shell. Remember that deep paging with large skip values gets slower on a real server, and range queries on a sorted key scale better.

Think of a pipeline as an assembly line

An aggregation pipeline passes documents through stages. $match filters, $project and $set reshape, $unwind turns one document with an array into one document per element, $group combines documents by a key with accumulators such as $sum and $avg, $sort orders and $limit cuts. Put $match as early as possible so later stages handle fewer documents.

In the playground every stage gets a button with the number of documents it produced. When the final result is empty, click through the stages from the first: the stage where the count drops to zero, or where a field becomes null, is where the problem is. This habit saves more time than any other.

Joins with $lookup

$lookup adds an array of matching documents from another collection, using localField and foreignField or a sub-pipeline with variables. Follow it with $unwind when each document should have exactly one match, and with $project to keep only the fields you need. On a real server, index the foreign field.

Rehearse updates safely

updateMany with $set, $inc, $push or $pull changes many documents at once, and mistakes are expensive. In the playground the change applies only to the in-browser copy, the result shows matched and modified counts, and undo restores the data. Rehearse the filter with countDocuments first, then run the update.

Limits

The playground is an emulator for learning and debugging. It has no indexes, transactions, collations, text or geospatial search, and a few advanced operators are missing, so always confirm important queries on a development copy of your real database. Performance in the browser says nothing about performance on a server with indexes and large data.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages