What is the most efficient way for pagination/infinite scroll?

I have a posts collection that a fetch with the query updatedAt:-1
I got two options using the last post and the updatedAt value of it
like find({updatedAt:{$lt:lastPostUpdatedAt}}).limit(10)
or I dont use that information but I can use skip instead
like find().skip(pageNumber).limit(10)

And one last thing ; _id and createdAt value, which one is faster to query in mongodb ?

which way would be faster ?

Hi @iktisat_ogrencisi ,

So doing range query rather than skip + limit is advised.

_id will be a good option if you sort the data by _id only but if you sort by date go with the date range. Be sure to index that of course.

See this article for more information:

Best regards,
Pavel

1 Like