T
T
triggerfinger2017-11-20 00:09:41
MongoDB
triggerfinger, 2017-11-20 00:09:41

Pagination: how to get the number of documents found?

There was a problem with the implementation of pagination.
I need to get a figure, the total number of documents found. The problem is that I'm using for pagination limitand skipI get not the total number of documents, but only the numberlimit

exports.fetchArticles = (req, res, next) => {
  let limit = parseInt(req.query.limit) || 10
  let page = parseInt(req.query.page) || 1
  Article.find({}, '-_id title content slug').sort({_id: -1}).limit(limit).skip((page - 1) * limit)
    .then((articles) => {
      console.log(articles.length)
      return res.status(200).json(articles)
    })
    .catch((err) => {
      return next(err)
    })
}

How to get the number of all documents found in Monge, and not just 10?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Khokhlov, 2017-11-20
@andrhohlov

https://docs.mongodb.com/v3.4/reference/method/db....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question