Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
Mongoose: Limiting Query result in MongoDB for Pagination

I got a collection with 1k documents and I want to paginate them, by limiting it with only 16 docs pr page, then use the skip method. How would I accomplish that with a URL query?

Right now my route looks like this, but doesn't work - I can query: localhost:3000/api/feed?isActive=true, but I cant limit it with localhost:3000/api/feed?isActive=true&page=16, as I want.

router.get('/feed', function(req, res, next) {
  var params = req.query;
  var page = req.query.page;
  var pagesize = 16

  Feed.find(params)
    .sort({'date' : 1})
    .skip(pagesize*(page-1))
    .limit(pagesize)
    .exec(function(err, result) {
      if (err) return next(err);
      res.json({
        confirmation: 'success',
        result: result
      })
    });
})
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You're using all query string parameters as properties to query:

var params = req.query;

Because you're now adding a new query string parameter page, this will affect your query. In your URL example, the query becomes this:

{
  isActive : 'true',
  page     : '16'
}

You should remove that parameter before using req.query as a query document:

var page = req.query.page;
delete req.query.page;

var params = req.query;
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!