Mongographic breakdown

I have a use case where I need to get a list of objects from mongo based on a request. But, to improve performance, I add Pagination. So, for the first call, I get a list of 10 objects, in the next I need 10 more. But I can not use offset and pageSize directly, because the first 10 objects displayed on the page can be changed [deleted].

The solution is to find the Object ID of the last transferred object and get the next 10 objects after the ObjectId.

Please help how to do this efficiently with Morphia mongo.

+7
source share
2 answers

Using morphine, you can do this with the following command.

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

Since you are requesting to retrieve items after the last identifier found, you do not have to worry about deleted items.

+7
source

One thing I came up with is that you will have the same problem as using skip() here if you are not going to change the way your interface works.

Using queries such as this requires a different type of interface, since now it’s more difficult for you to determine which page you are on and how many pages there are in the future, especially if you do this to avoid problems with regular paging.

The default interface type arising from this type of swap is just an endlessly scrollable page, think about comments on videos on YouTube or on Facebook or on Google+. There is no physical pagination or "page", instead you have a "More" button.

This is the type of interface you need to use to improve your ranking mode.

Regarding the @cubbuk request, it gives a good example:

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

Also, this should be greaterThan(lastId) , since you want to find everything above the last _id . I also sort _id if you don’t do your OjbectIds sometime before inserting the record, if so, then you can use the specific timestamp set for the insert.

+2
source

All Articles