2
votes

As per this link:

Supported Operations on DynamoDB

"You can query only tables that have a composite primary key (partition key and sort key)."

This doesn't seem correct though. I have a table in DynamoDB called 'users' which has a Primary Key that consists of only one attribute 'username'.

enter image description here

And I'm able to query this table just fine in NodeJS using only a 'KeyConditionExpression' on the attribute 'username'. Please see below:

var getUserByUsername = function (username, callback) {
var dynamodbDoc = new AWS.DynamoDB.DocumentClient();

var params = {
    TableName: "users",
    KeyConditionExpression: "username = :username",
    ExpressionAttributeValues: {
        ":username": username
    }
};

dynamodbDoc.query(params, function (err, data) {
    if (err) {
        console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
        callback(err, null);
    } else {
        console.log("DynamoDB Query succeeded.");
        callback(null, data);
    }
});

}

This code works just fine. So I'm wondering if the documentation is incorrect or am I missing something?

1
Yes, the documentation is very confusing. - Oin

1 Answers

4
votes

The documentation is correct.

"Partition Key and Sort Key – A composite primary key, composed of two attributes. The first attribute is the partition key, and the second attribute is the sort key. DynamoDB uses the partition key value as input to an internal hash function"

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DataModel.html

If a table doesn't have a sort key (range attribute), then the composite key is built from the hash key only. One of the results of that is that items won't be sorted as you like (items are sorted by sort key)