Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

189
Vistas
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
      })
    });
})
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

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;
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda