1
votes

Whenever i try to query a dynamodb table using C# document model, an exception will be raised as

do
{
    var request = new QueryRequest
    {
         TableName = "oauth_users",
         KeyConditionExpression = "user_id = :user_id",
         ExpressionAttributeValues = new Dictionary<string, AttributeValue> 
         {{":user_id", new AttributeValue { S =  userid_str }}},

         // Optional parameters.
         Limit = 1,
         ExclusiveStartKey = lastKeyEvaluated
     };

     var response = client.Query(request);

     // Process the query result.
     foreach (Dictionary<string, AttributeValue> item in response.Items)
     {

     }

     lastKeyEvaluated = response.LastEvaluatedKey;

} while (lastKeyEvaluated != null && lastKeyEvaluated.Count != 0);

Error :

Query condition missed key schema element: primary key

How can i query using a dynamodb key which is not a primary key but a global secondary index?

1

1 Answers

2
votes

Answer is we have to specify corresponding index name of global secondary index which we are trying to use in query. Eg: Included the IndexName parameter inside request which is a mandatory key to query a global secondary index column.

            do
            {
                var request = new QueryRequest
                {
                    TableName = "oauth_users",
                    KeyConditionExpression = "user_id = :user_id",
                    ExpressionAttributeValues = new Dictionary<string, AttributeValue> {{":user_id", new AttributeValue { S =  userid_str }}},
                    IndexName = "useridindex",
                    // Optional parameters.
                    Limit = 1,
                    ExclusiveStartKey = lastKeyEvaluated
                };

                var response = client.Query(request);

                // Process the query result.
                foreach (Dictionary<string, AttributeValue> item in response.Items)
                {

                }

                lastKeyEvaluated = response.LastEvaluatedKey;

            } while (lastKeyEvaluated != null && lastKeyEvaluated.Count != 0);

Hope it might help someone, Thanks. Have a great day