MongoDB 8 · Aggregation

Filter, sort and limit

Run $match early so the pipeline processes fewer documents.

db.orders.aggregate([
  { $match: { status: "paid" } },
  { $sort: { amount: -1 } },
  { $limit: 5 }
])

Official MongoDB 8 documentation

More aggregation commands

All aggregation commands · MongoDB 8 cheatsheet

Was this page helpful?