9

I'm using a python script to access a dynamodb database in AWS.

I have a table with a hash key and sort key.

For a given hash key, I want to find the item with the largest sort key that is less than a certain value. How can I do that?

Alternatively, is there a way to find the previous item from a given key?

I am not trying to find the item with the largest attribute value (an expensive task in dynamodb), I want the largest key value.

1 Answer 1

12

I found the answer,

import boto3
import botocore
from boto3.dynamodb.conditions import Key, Attr


dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(table_name)

response = table.query(
              Limit = 1,
              ScanIndexForward = False,
              KeyConditionExpression=Key('device').eq(device) & Key('epoch').lte(threshold)
           )

Where:

  • 'device' is my hash key
  • 'epoch' is my sort key
  • threshold is the value I want to search below
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.