Python – DynamoDB : The provided key element does not match the schema

amazon-dynamodbamazon-web-servicesbotopython

Is there a way to get an item depending on a field that is not the hashkey?

Example

My Table Users: id (HashKey), name, email

And I want to retrieve the user having email as 'test@mail.com'

How this can be done?

I try this with boto:

user = users.get_item(email='john.doe@gmail.com')

I get the following error:

'The provided key element does not match the schema'

Best Answer

The following applies to the Node.js AWS SDK in the AWS Lambda environment:

This was a rough one for me. I ran into this problem when trying to use the getItem method. No matter what I tried I would continue to receive this error. I finally found a solution on the AWS forum: https://forums.aws.amazon.com/thread.jspa?threadID=208820

Inexplicably, the apparent solution conflicts with all AWS documentation that I can find.

Here is the code which worked for me:

var doc = require('dynamodb-doc');
var dynamo = new doc.DynamoDB();

var params = { }
params.TableName = "ExampleTable";
var key = { "ExampleHashKey": "1" };
params.Key = key;

dynamo.getItem(params, function(err, data) {
    if (err)
        console.log(err);
    else
        console.log(data)
});