Limit doesn't work when query with context.fromquery in dynamodb

Viewed 2220

using c#, the code is like this

        DynamoDBContext context = new DynamoDBContext(client, new DynamoDBContextConfig() { TableNamePrefix = "lalala" });

        QueryFilter filter = new QueryFilter();
        filter.AddCondition("Userid", QueryOperator.Equal, "hashkeyvalue");
        QueryOperationConfig queryConfig = new QueryOperationConfig
        {
            Filter = filter,
            Select = SelectValues.AllProjectedAttributes,
            Limit = 1,
            IndexName = "Userid-UpdatedAtTimestamp-index"
        };
        try
        {
            var result = await context.FromQueryAsync<IAPRecord>(queryConfig).GetRemainingAsync();
            int ccc = result.Count;
        }
        catch (Exception ex)
        {
            throw new ArgumentException(ex.Message + ex.InnerException);
        }

and ccc should be 1 but now it equal to the whole set as if the Limit=1 doesn't exist.

Need helps!!

2 Answers

resolved.

var query = context.FromQueryAsync<IAPRecord>(queryConfig);
var result = await query.GetNextSetAsync();
int ccc = result.Count;

Apparently GetRemainingAsync will get all the results regardless of how many you set to the limit parameter in the query. Instead, we should have use GetNextSetAsync.

I had a similair problem and found this post useful. I used method QueryAsync with a global secondary index. I could not find a way to limit the result in the available overloads. By changing to the method FromQueryAsync I was able to limit my result and avoid timeout of my lambda. I think it is not really clear from naming what is the difference between the two methods, but FromQueryAsync has an overload that lets you provide both a QueryOperationConfig and a DynamoDBOperationConfig. The first is needed for limitation and the second is needed to specify GSI or overide tablename.

Related