Pagination in DynamoDB with Global Secondary Indices
<p>My DynamoDB schema is:</p>

<pre><code>TableName = 'Cities'
PrimaryIndex = '_id'
SecondaryIndex = 'areaId-timestamp-index'
  - areaId is primary key
  - timestamp is sort key
</code></pre>

<p>The data structure in DynamoDB looks as follows:</p>

<pre><code>{ _id: '....', name: 'New York', areaId: 22, timestamp: 1478882557071 },
{ _id: '....', name: 'Washington', areaId: 22, timestamp: 1478882557071 },
{ _id: '....', name: 'Copenhagen', areaId: 18, timestamp: 1478882557071 },
{ _id: '....', name: 'Berlin', areaId: 12, timestamp: 1478882557071 },
</code></pre>

<p>I want to be able to paginate through the cities under the following conditions:</p>

<ol>
<li>Only cities from, fx, area with id 22</li>
<li>Must be paginated in order according to timestamp</li>
</ol>

<p>My query looks like this:</p>

<pre><code>const areaId = 22

const params = {
  TableName: 'Cities',
  Limit: 10,
  IndexName: 'areaId-timestamp-index',
  KeyConditionExpression: 'areaId = :x',
  ExpressionAttributeValues: {
    ':x': areaId
  }
}

dynamo.query(params)
</code></pre>

<p>When I run this I get the expected response with a <code>lastEvaluatedKey</code></p>

<pre><code>LastEvaluatedKey = {
    areaId: 22,
    _id: '8c43-d917-f9ec',  // ID of last item in batch
    timestamp: 1478882561962  // Timestamp of last item in batch 
}
</code></pre>

<p>When I then run the next subsequent query (to get more results), I add the <code>ExclusiveStartKey</code> property with the exact same values as returned in <code>LastEvaluatedKey</code>.</p>

<p>However, when I run the query, I get:</p>

<pre><code>The provided key element does not match the schema
</code></pre>