Aggregation $limit and $skip
$limit passes on only the first n documents it receives; $skip drops the first n and passes on the rest. They take a single positive integer:
{ $limit: 5 }
{ $skip: 10 }$limit
Limit is most useful after a sort, to get a "top N" list. These are the three most expensive orders:
print(db.orders.aggregate([
{ $sort: { total: -1, _id: 1 } },
{ $limit: 3 },
{ $project: { customerId: 1, total: 1 } }
]).toArray());[
{ _id: 1015, customerId: 7, total: 121 },
{ _id: 1004, customerId: 5, total: 89 },
{ _id: 1018, customerId: 11, total: 83 }
]
When $sort is immediately followed by $limit, the server combines them into a top-k sort: it keeps only the best 3 documents in memory instead of sorting everything.
Without a sort, $limit simply takes whatever comes first, which is handy for peeking at the shape of documents while you build a pipeline:
print(db.customers.aggregate([{ $limit: 2 }, { $project: { name: 1, tier: 1 } }]).toArray());[
{ _id: 1, name: 'Ada Lovelace', tier: 'gold' },
{ _id: 2, name: 'Alan Turing', tier: 'silver' }
]
$skip for pagination
$skip + $limit page through results, just like cursor.skip().limit(). Always sort by something unique first so that pages are stable:
function productPage(page, size) {
return db.products.aggregate([
{ $sort: { price: 1, _id: 1 } },
{ $skip: (page - 1) * size },
{ $limit: size },
{ $project: { _id: 0, name: 1, price: 1 } }
]).toArray();
}
print(productPage(1, 3));
print(productPage(2, 3));[
{ name: 'Salted Pretzels', price: 3.5 },
{ name: 'Dark Chocolate Bar', price: 4.5 },
{ name: 'Oat Cookies', price: 5 }
]
[
{ name: 'Paper Filters (100)', price: 6 },
{ name: 'Almond Biscotti', price: 7.5 },
{ name: 'Chamomile Calm', price: 8 }
]
Order of stages matters
In find(), .limit() and .skip() are always applied in a fixed order. In a pipeline they run in the order you write them, and the results differ:
const ids = (pipeline) => db.orders.aggregate(pipeline).toArray().map(o => o._id);
print('skip 2, limit 3:', ids([{ $sort: { _id: 1 } }, { $skip: 2 }, { $limit: 3 }]));
print('limit 3, skip 2:', ids([{ $sort: { _id: 1 } }, { $limit: 3 }, { $skip: 2 }]));skip 2, limit 3: [ 1003, 1004, 1005 ]
limit 3, skip 2: [ 1003 ]
$limit: 3 then $skip: 2 keeps three documents and throws two of them away, leaving one.
Likewise, $limit before $match limits first and filters afterwards, which is rarely what you want:
const count = (pipeline) => db.orders.aggregate(pipeline).toArray().length;
print('match, then limit:', count([{ $match: { status: 'shipped' } }, { $limit: 5 }]));
print('limit, then match:', count([{ $limit: 5 }, { $match: { status: 'shipped' } }]));match, then limit: 3
limit, then match: 0
The second pipeline looked only at the first five orders, and none of them is shipped.
Test yourself
What does [{ $sort: { total: -1 } }, { $limit: 1 }] return?
- The smallest order
- The order with the highest total
- One random order
- All orders
With 20 sorted documents, how many does [{ $limit: 5 }, { $skip: 3 }] return?
- 2
- 3
- 5
- 17