I am using dynamoDb via dynamoose and trying to use filtering and sorting.
I had secondary Index as Partition key (Organization id) and sort key as (First name).
I want to sort with same column as well wants to filter with same column, I am using below query for:
users.query("organization_id").eq(user_organization_id).filter('first_name').conatins(first_name).sort('ascending').using('organization_id_first_name_index').exec();
Its gives me error
Filter Expression can only contain non-primary key attributes: Primary key attribute: first_name
Expected result is flitering and sorting will run smoothly
DynamoDB's Query operation has two distinct parameters -
KeyConditionExpressionandFilterExpression. The former is an efficient mechnism which can jump directly to the required keys, and the latter is a filter which goes over all the results returnd byKeyConditionExpression(you pay for reading all of them!) and decide for each of them whether or not it passes the filter.As DynamoDB told you,
FilterExpressiondoesn't want to work on the sort key, because it will be much more efficient for you to pick the sort key in theKeyConditionExpression.You could argue that you can't do the "contains()" with a
KeyConditionExpressionbut this shows that your data model is suboptimal - there is no way to implement your contains() request without reading the entire partition. Are you sure you really wanted to check contains() and not eq()?