Mongo db skip, but skip users matching the query

132 Views Asked by At

In my users collection, I am querying users who are over 18 years old, with a limit of 32. I am now working on a pagination feature. Page 1 should return the first 32 who are 18+ years old (with sorting by last_visit), page 2 would return the second 32 users part who are 18+ and so on.

Current code I have looks like:

db.bind('users').bind({
  search: function (query, fieldsLimit, page, callback) {
    var skip = page ? page * 32 : 0;

    this.find(query, fieldsLimit).skip(skip).limit(32).sort({ last_visit: -1 }).toArray(function (err, users) {
      callback(err, users);
    });
  }
});

Problem to fix: The skip here actually only skips any kind of user, so even if I am on page 2, it still queries many of the same users on page 1.

0

There are 0 best solutions below