Add simple pagination in Nest.js Mongoose
Graham Morby

Graham Morby @grahammorby

About: Hey guys, 15-year developer! I code with Vue.js and Python, I love to learn but also love to teach! So I try and write informative tutorials and posts. I am new to blogging and would love any feedback

Location:
Portsmouth UK
Joined:
Dec 9, 2019

Add simple pagination in Nest.js Mongoose

Publish Date: Apr 23 '23
16 2

I have been learning to wonderful world of nest.js recently and I want to say it's actually very cool, in fact its fast becoming a favorite of mine to use when building backend.

A thing I struggled with this week was adding pagination into one of my functions that allowed me to switch through my documents. I tried many packages and none of them seemed to do it correct. So I went away and tried to find out the best way to do it and I needed it to be pretty simple.

Now before I begin I must say this is not a fully fledged Pagination and wont give you all the functions you would want in most cases. But what this will do is give a simple way to move between stacks of data and have a page count.

So we are using MongoDB and Nest.js and I am assuming you have this set up and you are ready to go.

So we have a entity set up and we have out Controllers and Services all sorted. So lets start with the controller.

    @Get('get')
    async getdata(@Query() {limit, skip}) {
        return this.testService.getTests(
            skip,
            limit)
    }
Enter fullscreen mode Exit fullscreen mode

What we have above is a simple controller function that gives us a end point of '/get' and the returns the service passing in two params that are Skip and Limit. The skip and limit params are carried through the URL and the @Query picks those up.

/get?skip=8&limit=8
Enter fullscreen mode Exit fullscreen mode

So lets head over to the service so we can see what we are doing here

async getTests(
      skip = 0,
      limit = 8,
      ) {
      const count = await this.testModel.countDocuments({}).exec();
      const page_total = Math.floor((count - 1)/ limit) + 1;
      const data =  await this.testModel.find().limit(limit).skip(skip).exec();
      return {
        data: data,
        page_total: page_total,
        status: 200,
      }
  }
Enter fullscreen mode Exit fullscreen mode

Ok what we have here is the service taking the skip and limit and adding default values to them.

With the below line we count the documents and do some math to determine what the page count is.

const count = await this.testModel.countDocuments({}).exec();
      const page_total = Math.floor((count - 1)/ limit) + 1;
Enter fullscreen mode Exit fullscreen mode

Then we grab the data from the Mongo Database and pass our skip and limit to the find(). After all that we simple return our page totals and data.

Its a really simple way to have pagination and allows us to move through data.

Thanks for reading and have a fine Sunday!

Comments 2 total

  • Manuchehr
    ManuchehrFeb 26, 2024

    I'm not sure this is efficient code & also why should you return status?)

    • Mite
      MiteJan 21, 2025

      👋 Hi! Let me introduce Crudify, a library for NestJS that automatically generates CRUD endpoints with Mongoose. It's perfect for saving time and focusing on building amazing features. Check it out here: Crudify 🚀

Add comment