Filter by store and sort newest first
Set up a practice collection
Run this in mongosh on a development MongoDB instance with permission to write. Use an empty scratch database named keplergrid_compound so that only the default _id index is present at the start.
const scratch = db.getSiblingDB("keplergrid_compound")
const orders = Array.from({ length: 2400 }, (_, i) => ({
_id: i,
tenantId: "store-" + String(i % 60).padStart(3, "0"),
createdAt: new Date(Date.UTC(2026, 0, 1) + i * 60000),
total: 20 + i % 90
}))
scratch.orders.insertMany(orders)
Open keplergrid_compound.orders in KeplerGrid. On the Documents tab, enter the filter below. Open Sort & projection, enter the sort, and click Run. The first result in the practice collection has _id: 2347.
Filter
{ "tenantId": "store-007" }
Sort
{ "createdAt": -1 }
To capture a baseline plan for the same filter and sort, open Aggregation → JSON, paste the pipeline below, and click Explain. The last stage limits the example to five results.
[
{ "$match": { "tenantId": "store-007" } },
{ "$sort": { "createdAt": -1 } },
{ "$limit": 5 }
]
Create the compound index in KeplerGrid
- Use a Read & write connection with permission to create indexes. Open Indexes for the practice collection.
- Under Create an index, paste this key specification. Leave Unique values unchecked so the index allows repeated key combinations.
{ "tenantId": 1, "createdAt": -1 }
- Click Create index and confirm the database and collection. Verify that
tenantId_1_createdAt_-1appears in the index list.
tenantId goes first because the filter selects a single store. Within that store’s range of index entries, createdAt is already ordered newest first.
Check whether the index supplies the sort
Return to Aggregation → JSON and run Explain again on the same pipeline. In the winning plan, look for IXSCAN on tenantId_1_createdAt_-1, then check that the plan has no separate SORT stage. Expand Raw MongoDB explain (Extended JSON) if you need the full plan tree.
| Metric | Before index | After index |
|---|---|---|
| Scan and sort | COLLSCAN and SORT | IXSCAN and FETCH |
| Documents examined | 2,400 | 5 |
| Keys examined | 0 | 5 |
| Returned | 5 | 5 |
FETCH reads the full matching documents. The reduction in work here comes from going straight to one store’s ordered index entries and stopping after five. Compare plans and counters on your own data before deciding whether to add an index.
Adapt the field order to your query
- Sorting across all stores? With no equality filter on
tenantId, this index cannot provide globalcreatedAtorder. Test a timestamp-first index if that is your usual query. - Filtering on a range as well? Start with equality fields. Put sort fields before a separate range field when avoiding a sort matters most. Test range before sort if the range is highly selective.
- Already have a similar index? Compare the existing key order and winning plan before adding another one. Each additional index takes storage and adds work to writes.
See more MongoDB filter examples, learn to read an aggregation Explain plan, or check duplicate values before creating a unique index.
For a query that always targets a subset of the collection, test a partial index with Explain.