0

I keep reading posts saying not to use skip() and limit() because it will not scalate if there are more than 1000 entries in the MongoDB.

So, I'm actually new in this MERN thing and I'm trying to implement pagination but I'm not even sure how to properly do it. Here its my problem:

router.get('/male', async (req, res) => {
  const current_id = await Posts.find({}).sort({ _id: -1 }).limit(1);
  // const current_id;
  const N = '/api/posts/male';
  const page_size = 15;
  try {
    const posts = await Post.find({ _id: { $lt: current_id } }).sort({ date: -1 }).skip((N - 1) * page_size).limit(page_size).
      res.json(posts);
  } catch (err) {
    console.error(err.message);
    res.status(500).send('Server error');
  }
});

I got this example from this MongoDB ranged pagination and right now I'm just trying to implement it on mine. The links declares the const current_id without a value so I'm assuming you need to pass the last inserted id which is the reason for making a first request. The same goes(I had to give them a value) for the constants N and page_size.

Sorry for this but I'm literally new to this, what does the $lt means in the find()?

Right now its just displaying the 500 error in the Devtools console.

Thanks!

Rose
  • 35
  • 6
  • Comparison operator, returns values less than the set value. [Documentation](https://docs.mongodb.com/manual/reference/operator/query/lt/). – Chance Jun 29 '19 at 20:51
  • @JustCase already answer your doubt about `$lt`. Reading the post from where you got this example clearly says at the code comments that `current_id` is "id of first record on current page" and the functions are for "go to page current+N" and "go to page current-N" so `N` is the number of pages you want to go forward or backward. – virgiliogm Jun 29 '19 at 21:41

0 Answers0