result = dynamodb_table.query(
FilterExpression=filterexpression,
KeyConditionExpression=Key(lookup_key).eq(eval_value),
ScanIndexForward=False,
ExpressionAttributeValues=expressionAttribute,
)
FilterExpression = 'questionNumber = :questionNumber'
ExpressionAttributeValues = {':questionNumber': {'N': 1}}
Result is:
{'items': {'Items': [], 'Count': 0, 'ScannedCount': 8, 'ResponseMetadata': {'RequestId': 'HKA4854PGJ8K3IBG82B4I9LAMRVV4KQNSO5AEMVJF66Q9ASUAAJG', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'Server', 'date': 'Wed, 16 Mar 2022 19:10:23 GMT', 'content-type': 'application/x-amz-json-1.0', 'content-length': '39', 'connection': 'keep-alive', 'x-amzn-requestid': 'HKA4854PGJ8K3IBG82B4I9LAMRVV4KQNSO5AEMVJF66Q9ASUAAJG', 'x-amz-crc32': '61672736'}, 'RetryAttempts': 0}}}
Items count is 0, which should be 1 (one row should match, its in the results), ScannedCount is correct. What is wrong can any one tell?
Sample output without Filter Expression is:
{'items': {'Items': [{'questionType': 'checkbox', 'question': 'Fitness Goals', 'questionOption': "['Fat loss', 'Muscle gain', 'Improve overall health', 'Look and feel better', 'Improve sports performance']", 'questionAnswer': 'improveHealth,lookBetter', 'email': '[email protected]', 'questionNumber': Decimal('1')}], 'Count': 8, 'ScannedCount': 8, 'ResponseMetadata': {'RequestId': '50JPHPTV4TUQEBUPC3TJ77TUKBVV4KQNSO5AEMVJF66Q9ASUAAJG', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'Server', 'date': 'Wed, 16 Mar 2022 20:58:06 GMT', 'content-type': 'application/x-amz-json-1.0', 'content-length': '2271', 'connection': 'keep-alive', 'x-amzn-requestid': '50JPHPTV4TUQEBUPC3TJ77TUKBVV4KQNSO5AEMVJF66Q9ASUAAJG', 'x-amz-crc32': '2489860921'}, 'RetryAttempts': 0}}}
I have removed 7 items. Seems to me questionNumber:Decimal('1')
in above is creating issue. How to filter/fix it?
CodePudding user response:
ExpressionAttributeValues = {':questionNumber': {'N': 1}}
The attribute value's N property should hold a string representation of the number, instead of the numeric representation.
CodePudding user response:
After playing with conditions inside FilterExpression i got error "Invalid Operand type M". So i searched that and come to know from this link Answer from Bastien answer What i was doing wrong is
ExpressionAttributeValues = {':questionNumber': {'N': 1}}
it should have to be
ExpressionAttributeValues = {':questionNumber':1}