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:
 B、C、E、D
 Now you need to sort by name, 3 pieces per page, then the data to be retrieved is:
 E、D、C
 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: c.name}}

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, ifc.name = b.name, 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: c.name}, _id: {$gt: c._id}}