Using limit() with Mongoose Queries

Aug 3, 2022

Mongoose's limit option allows you to limit the amount of results from the query. The easiest way to set the limit option is using the limit() method as follows.

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
  name: String
});

const Test = mongoose.model('Test', testSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  for (let i = 0; i < 10; i++) {
    await Test.create({
      name: 'Test' + i
    });
  }
  console.log(await Test.find().limit(2)); // returns a maximum of two documents
}
run();

Sorting

We recommend always using limit() with sort(). If you don't specify sort(), the MongoDB server doesn't guarantee you'll get the results back in any particular order. The MongoDB server applies sort before limit, so MongoDB will sort the full result set and give you the first limit results in order.

await Test.find().limit(5); // returns a maximum of 5 documents, could be in any order
await Test.find().sort({ createdAt: -1 }).limit(5); // returns the first 5 documents created

Using callbacks

If you are using callbacks, make sure to call limit() before passing in a callback. Failure to do so will result in the query executing without a limit as seen here.


Want to become your team's MongoDB expert? "Mastering Mongoose" distills 8 years of hard-earned lessons building Mongoose apps at scale into 153 pages. That means you can learn what you need to know to build production-ready full-stack apps with Node.js and MongoDB in a few days. Get your copy!

Did you find this tutorial useful? Say thanks by starring our repo on GitHub!

More Mongoose Tutorials