2

I have a DynamoDB query that works fine on the AWS Console but it doesn't on code.

Here is my query on the console: enter image description here

Now here is my c# code to query it:

var query = new QueryOperationConfig
            {
                KeyExpression = new Expression
                {
                    ExpressionStatement = "#pkey = :v_pkey and #skey >= :v_skey",
                    ExpressionAttributeNames = {
                        { "#pkey", "MailingId" },
                        { "#skey", "RegistroCarteiraId" },
                    },
                    ExpressionAttributeValues = new Dictionary<string, DynamoDBEntry>()
                    {
                        { ":v_pkey", new Primitive("62", true) },
                        { ":v_skey", new Primitive("00e0bbfc-aed0-4f0e-acef-a3623a9f9694") },
                    },
                },
                BackwardSearch = false,
                ConsistentRead = true,
                Limit = 1,
                FilterExpression = new Expression
                {
                    ExpressionStatement = "#psituacao = :v_psituacao and attribute_not_exists(#pdisponibilidade)",
                    ExpressionAttributeNames =
                    {
                        { "#psituacao", "Situacao" },
                        { "#pdisponibilidade", "Disponibilidade" }
                    },
                    ExpressionAttributeValues =
                    {
                        { ":v_psituacao", new Primitive("1", true) },
                    }
                }
            };

            var search = table.Query(query);
            var docs = await search.GetNextSetAsync();

I get no errors, only an empty array as the result. If I change the sort key to different values, it works, but for this particular value it does not...

I've been at it all day and couldn't figure it out what is wrong.

Any help will be much appreciated.

Thanks

1 Answer 1

1

The problem was the LIMIT 1.

As I found out, the filter only happens on the fetched items and, since I was only fetching 1 item, when the filter occurred, the result had no records that matched the criteria.

Removing the Limit 1 solved the problem.

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

2 Comments

Adding a bit more info. LIMIT controls how many items the service evaluates before returning. SDK doesn't do anything with the value. So if you set it to 1 the service would look at the first item in the "MailingId" partition see if it matches the sort key query and the filter expression and return. If the item didn't evaluate to true then it would return 0 items. Not that you should do this but you could have kept calling GetNextSetAsync and it would have paged through the items in the "MailingId" partition and would eventually return back your item. Just not an efficient way of searching.
That is right! I only realized it later... but in the end I changed my data so that I could get what I need without paging or etc. Great clarification though!

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.