0

I am trying to query my dynamodb with contains comparison parameter, but it is not working

data= table.query(
    IndexName='farm_name-index',
    KeyConditionExpression = Key('user_name').eq(user_name)& contains(Key('farm_zone_id_time_stamp'),farm_zone_id_time_stamp)& Key('time_stamp').between(from_time,to_time) 
    )

and i am getting the following error :

name 'contains' is not defined: NameError
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 25, in lambda_handler
    KeyConditionExpression = Key('user_name').eq(user_name)& contains(Key('farm_zone_id_time_stamp'),farm_zone_id_time_stamp)& Key('time_stamp').between(from_time,to_time)
NameError: name 'contains' is not defined

can any one tell me the right way to doit ?

1 Answer 1

2

You cannot do contains on a sort key. You can do equality, begins_with, or comparison operators only.

Here's the list from the docs: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html#Query.KeyConditionExpressions

Why? Because these are the operators that are efficient for pulling data out of a sorted list, such as is put under the sort key.

With DynamoDB you can use a filter expression for things like contains. It's not index optimized.

Sign up to request clarification or add additional context in comments.

1 Comment

It is important to note however that you can not use sort key in filter expression. More details here: repost.aws/questions/QUW97JOu44QGWUZehSSw0Mjw/…

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.