Is it possible to ORDER results with query or scan in DynamoDB?

前端 未结 6 772
长发绾君心
长发绾君心 2020-12-04 16:42

Is it possible to ORDER results with Query or Scan API in DynamoDB?

I need to know if DynamoDB has something like [ORDER BY \'field\'] from SQL queries?

Tha

6条回答
  •  孤城傲影
    2020-12-04 17:16

    If you are using boto2 and you have the sort key on one of the columns in your table, you can sort what you retrieve in order or in reverse order by saying:

    result = users.query_2(
        account_type__eq='standard_user',
        reverse=True)
    

    If you are using boto3 and you have the sort key on the column that you want to sort the result by, you can sort the data you retrieve by saying:

    result = users.query(
        KeyConditionExpression=Key('account_type').eq('standard_user'),
        ScanIndexForward=True)
    

    Remember in boto3 if ScanIndexForward is true , DynamoDB returns the results in the order in which they are stored (by sort key value). This is the default behavior. If ScanIndexForward is false, DynamoDB reads the results in reverse order by sort key value, and then returns the results to the client.

提交回复
热议问题