简体   繁体   中英

How to use GSI in dynamodb?

I am using AWS Console and NodeJS.

I have the dynamodb table of users with partition key (user_id) and sort key (company_id) and other attributes.

One of my attributes is email of user. Email is unique attribute.

I need to get user_id by email but I haven't his user_id and company_id.

I think that I should use a Global Secondary Index.

I clicked on the users table, opened the Indexes tab and created GSI for this table. (name: email, type: GSI, Partition Key: email string, attributes: user_id)

I am using method Query from documentClient. This is my payload:

payload = {
    "TableName": "users",
    "IndexName": "email",
    "KeyConditionExpression": "#index = :index_value",
    "ExpressionAttributeNames":{
        "#index": "email"
    },
    "ExpressionAttributeValues": {
        ":index_value": {"S": "test@gmail.com"}
    },
    "ProjectionExpression": "user_id",
        "ScanIndexForward": false
    };
}

This is my error from CloudWatch:

"errorMessage": "One or more parameter values were invalid: Condition parameter type does not match schema type"

I have found a solution while I was writing this question. So as I use documentClient my payload should looks like this

payload = {
    "TableName": "users",
    "IndexName": "email",
    "KeyConditionExpression": "#index = :index_value",
    "ExpressionAttributeNames":{
        "#index": "email"
    },
    "ExpressionAttributeValues": {
        ":index_value": "test@gmail.com" // <----------------
    },
    "ProjectionExpression": "user_id",
        "ScanIndexForward": false
    };
}

Hope it helps to someone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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