The Problem of Data Scroll Paging

  mongodb, question

I want to use last_id method to realize scrolling paging now, but there are some problems

The original data of the data is:
 Now you need to sort by name, 3 pieces per page, then the data to be retrieved is:
 Then when scrolling to the second page, according to last_id, that is, c _id, e and d are fetched, thus the order is out of order

Is there any solution, thank you

What you said herelast_idDon’t define it as must be_id. Its principle is to find the last bit of the sort field and start the next page from that one. So since it is based onnameSort, then yourslast_idIn fact, it should be C ‘s.nameNext time

{name: {$gt:}}

However, there is a problem with this paging method, which requires that the sorted fields must be unique, or some documents may be skipped. For example, =, then the above query will obviously skip the record b. Therefore, if the sorted fields are not unique, a second sorted field should be added, such as_id

{name: 1, _id: 1}

The corresponding filtering conditions should be changed to:

{name: {$gte:}, _id: {$gt: c._id}}