0

I am writing a Dynamo Db .NET query with both "Limit" as well as "Filter Expression" as shown below:

var search = testTable.Query(new QueryOperationConfig
                {
                    Filter = new QueryFilter("Name", QueryOperator.BeginsWith,  "B"),
                    IndexName = "IsActive-gsi",
                    Limit = 5,
                    PaginationToken = paginationToken,
                });

I expect that filter expression should be evaluated first and then limit condition is applied to the filtered result. But query seems to work in other way. That is, Limit condition is getting applied first and then filter expression is applied on the limited result. Due to this, I am not getting the expected result.

For example, say I have 10 rows in a table (with only one column of string type). Say first 5 rows starts with letter "A" and then 6th row starts with letter "B". I have added Limit as 5 and filter expression as "BeginsWith('B')" as shown above in the query. I expect to get at least one result(6th row) but query returns zero results.

My question: Is my query wrong or does Dynamo DB works this way? If it works this way, then how to get around this issue i.e. first apply filter expression on all rows and then apply limit condition, both in a single call to database?

1 Answer 1

2

FilterExpression do not actually change the query itself (i.e. all elements that match your query are returned from DynamoDB), but only limit the results afterwards. See the FilterExpression documentation:

A filter expression is applied after a Query finishes, but before the results are returned.

So this means, that your observed behaviour is the expected behaviour.

I think to get the result you want, you need to make sure that the attribute on which you want to apply a filter is part of the key (i.e. probably the sort key), then you can use Key Condition Expressions.

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

1 Comment

Thanks for the answer. This explains why I am not getting the expected result. Clearly, I have to use Key Condition Expression and in case of querying for multiple attributes I may need to use different approach as suggested, for ex, here: aws.amazon.com/blogs/database/…

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.