简体   繁体   English

AWS Dynamodb:是否可以仅使用排序键进行查询

[英]AWS Dynamodb: Is it possible to make a query only using sort key

在此输入图像描述

Hi dynamodb community, 嗨dynamodb社区,

So the table in question is MLA_USER_AUTH. 所以有问题的表是MLA_USER_AUTH。 As you see it has as partition key and as sort key. 如您所见,它具有分区键和排序键。 I want to be able to query the database using both user_id and email. 我希望能够使用user_id和email查询数据库。

Sometimes I need to find the user associated with the email and sometimes the userid. 有时我需要找到与电子邮件关联的用户,有时还需要找到用户ID。 Both email and userid attributes are unique for every visitor. 电子邮件和用户标识属性对于每个访问者都是唯一的。 is there any way it can be done? 有什么办法可以做到吗?

This is my current query. 这是我目前的查询。 This obviously doesnt work. 这显然不起作用。 As it throws the error: ValidationException: Query condition missed key schema element: UserID 因为它抛出错误:ValidationException:查询条件错过了键架构元素:UserID

var params = {
        TableName : 'MLA_USER_AUTH',
        KeyConditionExpression: "Email = :email",
        ExpressionAttributeValues:{
            ":email": { "S" : email } 
        }
    };
    dynamodb.query(params, function(err, data) { 

Are GSIs my only option. GSI是我唯一的选择。 Any help appreciated 任何帮助赞赏

Thanks, Ninjasoar 谢谢你,Ninjasoar

A Secondary Index would do want you want. 二级指数确实想要你想要的。

// definition
{
    TableName: "MLA_USER_AUTH",
    AttributeDefinitions:[
    {
        AttributeName: "Email",
        AttributeType: "S"
    }
    ],
    GlobalSecondaryIndexUpdates: [
    {
        Create: {
            IndexName: "emailIndex",
            KeySchema: [
                {AttributeName: "Email", KeyType: "HASH"}
            ]
        }
    }
    ]
}

// Query
{
    TableName : "MLA_USER_AUTH",
    IndexName: "emailIndex",
    KeyConditionExpression: "Email = :email",
    ExpressionAttributeValues:{
        ":email": { "S" : email } 
    }
}

See http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.JsShell.06.html for more information on creating them. 有关创建它们的更多信息,请参见http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.JsShell.06.html

As Mark B said, you need partition-key to query - you can't do it using sort-key alone. 正如Mark B所说,你需要使用partition-key来query - 你不能单独使用sort-key来实现。

So, the only way out for you is to create a GSI with email as the partition-key. 因此,唯一的出路是创建一个以email作为分区密钥的GSI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM