0

I have this problem , i don't know how to make a pagination in Mongoose text search module, i am totally beginner, so please help, this is my code:

searchModul.create({ title: title, description: description }, function (err) {
  if (err) return handleError(err);
  searchModul.textSearch(title, function (err, output) {
    if (err) return handleError(err);
    res.render('search', { 
          title   : 'title',
          results  : output.results
    });
  });
}); 

and also i want to know how to display that pagination in the search.ejs view. and thanks in advance.

er_web-pr
  • 55
  • 1
  • 5
  • I'm sorry, I'm not understanding your question as it is worded. Is your mongoose query returning more data that some limit, and you want to access the rest of your query? – Sze-Hung Daniel Tsui Jun 06 '15 at 15:49
  • no the query return more than 300 documents . so i want to add a pagination . for example 20 documents per page – er_web-pr Jun 06 '15 at 17:47

3 Answers3

0

To implement pagination, use $limit to define a limit for each query, and $skip to navigate pages.

From the docs:
$limit takes a positive integer that specifies the maximum number of documents to pass along.

$skip takes a positive integer that specifies the maximum number of documents to skip.

There are previous questions like this one, check out the answers here and a more detailed tutorial here.

Hope that helps!

Community
  • 1
  • 1
Sze-Hung Daniel Tsui
  • 2,282
  • 13
  • 20
0

I think you want something like this:

  searchModul.find({ $text: { $search: title }}).skip(50).limit(50).exec(function (err, output) {
    if (err) return handleError(err);
    res.render('search', { 
          title   : 'title',
          results  : output.results
    });
  });

This will return the second 50 items that match. .skip(50) the number of items skip and .limit(50) is the number of items to return.

Brmmmm
  • 152
  • 7
0
const field = req.query.field;
const search = {};
search[field] = req.query.searchValue;
search["role"] = req.params._role;
search["status"] = parseInt(req.query.status);
user = await Users.paginate(search, {
    limit: parseInt(req.query.limit),
    page: parseInt(req.query.page),
});

OR

var ObjectId = mongoose.Types.ObjectId;
let _id = new ObjectId(req.query.q);;
user = await Users.paginate(
{
    role: req.params._role,
    _id: _id,
},
{
    limit: parseInt(req.query.limit),
    page: parseInt(req.query.page),
});
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Tyler2P Feb 16 '21 at 18:42