Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query 'Not Equal to' in boto3 dynamoDB python

I am new to AWS and python. I have been trying to get the records from dynamoDB using boto3 in python. Could anyone please suggest how to get records when value1 not equal to value2. I am able to query with eq but not ne. Below is my code.

response = table.query(
    KeyConditionExpression=Key('Id').eq('123') & Key('status').ne('Booked')
)

I am getting below error when I execute the above code.

Lambda execution failed with status 200 due to customer function error: 'Key' object has no attribute 'ne'.
like image 477
Sam Avatar asked Sep 13 '25 10:09

Sam


1 Answers

KeyCondition don't include ne operator for the sort key https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LegacyConditionalParameters.KeyConditions.html

You will need to user FilterExpression instead :

FilterExpression : 'status <> :Booked', ExpressionAttributeValues : {':Booked' : 'Booked'}

like image 112
Ghamgui Khaled Avatar answered Sep 16 '25 00:09

Ghamgui Khaled