Read an aggregation pipeline stage by stage with ordering problems flagged, and take an ObjectId apart to find the creation time it carries.
Waiting for a pipeline
Paste an aggregation pipeline. Nothing is executed and no database is contacted.
Waiting for a pipeline
Paste an aggregation pipeline as a JSON array.
MongoDB Pipeline Inspector runs entirely on your device — nothing is uploaded, and no database is contacted.
You’re on the Free plan. Compare plans
Stage order is the whole performance story in an aggregation pipeline. A `$match` before a `$group` filters documents before they are aggregated and can use an index; the same `$match` after the `$group` filters the results and cannot. The pipeline is correct either way, and the difference is between a query that touches an index and one that scans the collection.
`$lookup` is not a join, however much it reads like one. It runs once per input document, so a `$lookup` following an unfiltered `$match` is one query per document in the collection. Filtering first is not an optimisation here — it is the difference between usable and not.
`$project` and `$unset` early reduce the documents flowing through every later stage, which matters most right before a `$lookup` or a `$sort`. A `$sort` that cannot use an index has a memory limit and fails rather than slowing down when it exceeds it.
An ObjectId carries its creation time in its first four bytes, which has two useful consequences: a separate `createdAt` field is often redundant, and sorting by `_id` is sorting by insertion time — something people rely on without always knowing why it works.
That also makes an ObjectId usable as a range boundary. Generating one for a date lets you filter by insertion time without a second index, which is why the tool will build one for you.
Not always, but as early as possible. A `$match` before a `$group` can use an index and reduces what gets aggregated; after a `$group` it filters results in memory. Anything that reduces documents belongs as early as it can correctly go.
Because it executes per input document. With no filtering before it, that is one lookup per document in the collection. Filter first, and make sure the foreign field is indexed — `$lookup` will happily do a collection scan for each document otherwise.
Often not. An ObjectId's first four bytes are a Unix timestamp, so `_id` already carries creation time to the second. A separate field is worth it when you need better precision, or when the document can be created with a non-default id.
Yes, and it is a neat trick. Build an ObjectId whose timestamp is your boundary and compare against `_id` — you get a date range filter using the primary key index, with no second index needed.
No. The pipeline is parsed as text in your browser. No MongoDB server is contacted and no query is executed.