title: How to do pagination in nodejs+express+mongodb
body: <p>I am using nodejs+express+mongodb. I am working on api. I want to do the pagination in nodejs+express.
  Note: there is mongoose-pagination option but I am not using mongoose. I am using direct mongodb only.</p>
  
  <p>Please tel me how can I do the pagination in nodejs+express. I got this link <a href="https://github.com/expressjs/express-paginate" rel="nofollow noreferrer">express-paginate</a></p>
  
  <p>But I could not work on it. I dont know How can I implement it in my API.</p>
  
  <p>Here is JSON data. In this steps arrays listing more than 10 so I want to do pagination for this.</p>
  
  <p>Here is my API. Here inside steps for loop, I added given(in the above github link) pagination code</p>
  
  <pre><code>const get = (req, res, next) =&gt; {
  applogger.verbose(`retrieving asdsa with id ${req.params.asd}`);
  asd.findOneById(req.params.asd)
      .then(function(resp) {
          for (var i = 0; i &lt; resp.steps.length; i++) {
              if (resp.asd[i].image == null) {
                  resp.asd[i].image = imgurl;
              }
  
              asd.paginate({}, { page: req.query.page, limit: req.query.limit }, function(err, users) {
  
                  if (err) return next(err);
  
                  res.format({
                      json: function() {
                          res.json('users', {
                              users: users.docs,
                              pageCount: users.pages,
                              itemCount: users.limit,
                              pages: paginate.getArrayPages(req)(3, users.pages, req.query.page)
                          });
                      },
                      json: function() {
                          // inspired by Stripe's API response for list objects
                          res.json({
                              object: 'list',
                              has_more: paginate.hasNextPages(req)(users.pages),
                              data: users.docs
                          });
                      }
                  });
              });
          }
  
          if (resp.asd == null) {
              resp.asd = imgurl;
          }
          res.json(resp);
      }, function(err) {
          res.send(err);
      });
  };
  </code></pre>