I have a dynamodb table on which a GSI is defined with a partition key and sort key.
Let’s say the parition key is name
and sort key is ssn
for the GSI.
I have to fetch based upon a name and ssn, below is the query I am using and it works fine.
table.query(IndexName='lookup-by-name',KeyConditionExpression=Key('name').eq(name)
& Key('ssn').eq(ssn))
Now, I have to query based upon a name and a list of ssns.
For Example
ssns=['ssn1','ss2','ss3',ssn4']
name='Alex'
query all records which has name as ‘Alex’ and whose ssn is present in ssns list.
How do I implement something like this ?
3
Answers
Ended up using just the name as keycondition and then filter out the ssn in python code.
Below worked for me as the number of records was not a lot.
You would have to do multiple queries.
While DynamoDB native SDK cannot provide the functionality to do this, you can achieve it using PartiQL which provides a SQL like interface for interacting with DynamoDB.
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ql-gettingstarted.html
It would also require you to use the lower level
Client
instead of theTable
level resource which you are using above.