C#dynamodb错误:查询条件错过了关键架构元素:主键

时间:2017-11-23 12:49:56

标签: c# amazon-dynamodb

每当我尝试使用C#文档模型查询dynamodb表时,都会引发异常

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);

错误:

  

查询条件错过了关键架构元素:主键

如何使用不是主键但是全局二级索引的dynamodb键进行查询?

1 个答案:

答案 0 :(得分:1)

答案是我们必须指定我们在查询中尝试使用的全局二级索引的相应索引名称。例如:在请求中包含IndexName参数,该参数是查询全局二级索引列的必需键。

            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);

希望它可以帮到某人,谢谢。祝你有美好的一天