Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo pagination

I have a use case where I need to get list of Objects from mongo based off a query. But, to improve performance I am adding Pagination. So, for first call I get list of say 10 Objects, in next I need 10 more. But I cannot use offset and pageSize directly because the first 10 objects displayed on the page may have been modified [ deleted ].

Solution is to find Object Id of last object passed and retrieve next 10 objects after that ObjectId.

Please help how to efficiently do it using Morphia mongo.

like image 245
rl99 Avatar asked Jan 29 '13 18:01

rl99


1 Answers

Using morphia you can do this by the following command.

datastore.find(YourClass.class).field(id).smallerThan(lastId).limit(10).order("-ts");

Since you are querying for retrieving the items after the last retrieved id, you won't be bothered to deal with deleted items.

like image 128
cubbuk Avatar answered Sep 20 '22 14:09

cubbuk