When I am implementing infinite scroll or pagination with mongo, I am using the following technique (I hope you will be able to adapt it to your doctrine).
If for example I have to show X
elements per scroll/page. I am selecting X+1
element. Then if exactly X+1
element was selected I am selecting new X+1
element (skipping the first X
). But I output always only X
elements.
If you have less than X+1
, I output all of them and know that there is nothing else to show.
Another approach is not to bother with all this +1
thing and only get X
elements and stop right when you encounter an empty result. After this point of time, you just stop sending requests.