0

I've implemented a scan query that fetches all the users from DynamoDb using NodeJS. Now I want to implement sorting on createdAt to bring the latest user or oldest user from DynamoDb. Still, as per doc, we can't apply any sorting on the scan query of DynamoDB so does anyone have an idea how can we implement sorting on the scan query of DynamoDB?

// Prepare param for scan table
let scanParam = {
    TableName: userTableName,
    FilterExpression: FilterExpressionRef.join(' AND '),
    ExpressionAttributeValues: ExpressionAttributeValuesRef,
    consistentRead : false,
    ProjectionExpression: 'id, username, email, isCreator, displayName, gender, locationCountry, locationCity, coinBalance, coinPerHalfYear, coinPerMonth, coinPerYear, followerCount, followingCount, createdAt, updatedAt',
}
Parth Shah
  • 51
  • 7

1 Answers1

1

If I am not mistaken, then sorting using the scan() operation is not possible or at least not straightforward.

What one can do is to use the query() operation with a primary query that is a combination of partition and sort key, or GSI (Global Secondary Index) table.

Adding a sort key to a table gives more data retrieval functionality beyond scans and just partition keys. In the query, one can write the conditional statement using the KeyConditionExpression, and retrieve the result over a range, then one can locally sort (using nodejs sorting operation) the result to the desired result.

One can also go the GSI path, create a GSI index that contains a sub-set of the needed attributes (in this case "createdAt", with datatype Number) from the base table, and run the query with the ScanIndexForward parameter set to false/true as per the requirement for ascending/descending order.

It really depends on your use case and how pragmatic approach you want.

For more information, you can read about sort keys here, and GSI here.

Roshan Br
  • 362
  • 2
  • 17