Aditya V
Aditya V

Reputation: 578

How to do backward pagination in dynamoDB with LastEvaluatedKey?

  param.FilterExpression = "#black_listed = :black_listed_val";
  param.Limit = 5
  param.ExpressionAttributeValues = {":black_listed_val": body.blacklisted};
  param.ExpressionAttributeNames = {"#black_listed": "black_listed"}
  param.ScanIndexForward = true
  param.ExclusiveStartKey = {"id": "11931258-b582-4d2d-98d9-aa8ae0fe2e43"}

I can do forward pagination with this code, but how could I go back?

Upvotes: 0

Views: 1287

Answers (2)

Jason Wadsworth
Jason Wadsworth

Reputation: 8887

To page backward you need to set ScanIndexForward to false AND the ExclusiveStartKey to the key of the first item of the second page. As an example, say your first page is items 1, 2 & 3. The response from that will include a LastEvaluatedKey for 3. You can use that to go forward by passing that to the ExclusiveStartKey, resulting in 4, 5 & 6. Going backward you can't do that, because using that same key would result in just 2 & 1. Instead you need to use the key of 4 as your ExclusiveStartKey.

Upvotes: 4

Horatiu Jeflea
Horatiu Jeflea

Reputation: 7404

Change

param.ScanIndexForward = true

to

param.ScanIndexForward = false

Upvotes: 0

Related Questions