0

Since ObjectId contains datetime so i was using _id to filter records by date range.

like

{
 "_id" : { 
           "$gte" :  ObjectId("5aeed1be0000000000000000"),
           "$lte" :  ObjectId("5aeed1be0000000000000000"),
         }
}

Now i would like to apply pagination too by _id for fast pagination i am preventing the use of skip.

is that possible for me to add another $gte or $lte filter on _id field.

thanks.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
john cena
  • 191
  • 1
  • 1
  • 7
  • You can get rid of `skip()` but you're still going to `limit()`. So basically keep track of the last `_id` seen on the `limit()` boundary. More details on the referenced question, but it's a bit easier when you're using `_id` as the range already since you would be ( and should be ) sorting on that same field anyway. It just means advancing the `$gt` value for `_id` to the last seen on each page. – Neil Lunn Jun 03 '18 at 13:13
  • I have not misunderstood anything. Please read the referenced answer and learn something. You keep the last `_id` value from the page and use it as the new starting point in the range. The process for paging without skip is explained with detail on the provided linked answer. – Neil Lunn Jun 03 '18 at 13:25
  • john is correct this question is not a duplicate. – cool cool Jun 03 '18 at 13:32
  • @coolcool It's a duplicate. You create a range starting at one ObjectId value representing a date and ending at another ObjectId for the end date. You take a page with a limit, you then start your next range with `$gt` starting at the last seen _id`. Which is exactly what the linked answers say. – Neil Lunn Jun 03 '18 at 13:35

0 Answers0