1

I need to implement efficient pagination for Cosmos DB with nodejs api. There are many examples about the implementation with .NET and LINQ but I could not find anything good for nodejs. The idea is to send the pageSize and pageIndex and get the relevant result.

I already know we can always use dbClient.queryDocuments and get the queryIterator and perform the pagination but this requires always iterating from the first document in the DB. An example could be find here.

Any idea how to do it in an efficient way?

José Pedro
  • 1,097
  • 3
  • 14
  • 24
Mori
  • 2,484
  • 5
  • 28
  • 45

1 Answers1

1

Unfortunately CosmosDB as an engine doesn’t have skip and take pagination support yet.

It is, however, a planned feature.

The blogs you’ve read provide one of the few viable workarounds for now which of course comes with a cost.

You could write something smarter and instead of iterating though every document from the beginning, you could keep the request’s continuation token and use it with your next request. That way you can have a previous and next button logic.

Nick Chapsas
  • 6,872
  • 1
  • 20
  • 29
  • Keeping continuation token means keeping state. That was something I was trying to avoid. But apparently there is on way around it. – Mori Sep 11 '18 at 09:10
  • Depends on your requirements yeah. The state doesn’t have to be kept on the server. In one of my cases I keep it on the client and I send it with the request as an option. It comes down to your use case of course, but yeah CosmosDB doesn’t truly have skip take pagination. – Nick Chapsas Sep 11 '18 at 10:21
  • 1
    The continuation token is no more state to maintain than what page you are on. How would you do it if CosmosDB had skip and take pagination support? – Larry Maccherone Sep 11 '18 at 15:05
  • Note, with the continuation token, you are better off setting the suggested page size to -1 (as many as it can get before timing out). I've found that I rarely need the continuation token as it almost always comes back with everything for my use cases. – Larry Maccherone Sep 11 '18 at 15:07