0
votes

I'm completely new to DynamoDB, and I can't find out how query works.
So, in my case, I have a table(collection/etc) Users with this fields and AWS types in brackets:
id [type String], name [type String], email[type String], ref_code[type Map], register_date[type String]
And my table Indexes are

KeySchema: [
  { AttributeName: 'id', KeyType: 'HASH' },
  { AttributeName: 'register_date', KeyType: 'RANGE' },
],
AttributeDefinitions: [
  { AttributeName: 'id', AttributeType: 'S' },
  { AttributeName: 'register_date', AttributeType: 'S' },
],

I've read documentation here, here, here and a lot of other info too, but still can't understand how can I query user by his/her name.
So, in MySQL world if I have primary index on field Id, I still can query user data by his/her name, like this SELECT * FROM users WHERE name = '<name>';
But, this behaviour isn't work with DynamoDB.
I've tried to query like this:

var params = {
TableName: 'Users',
IndexName: 'name',
KeyConditionExpression: '#u_name = :name',
ProjectionExpression: '#u_name, lives, fb_account',
ExpressionAttributeNames: {
  '#u_name': 'name',
},
ExpressionAttributeValues: {
  ':name': 'Mykhaylo',
},
};

And a lot of other options, but nothing worked out.
So, my question is How to make query in AWS DynamoDB?
EDIT
If I need to set more than 5 global second indexes, how should I perform my query?
Is it possible at all?

2

2 Answers

1
votes

You cannot directly query on the field which is not hash key. You have to either use scan with filter on name like

var params = {
    TableName: 'Users',
    FilterExpression: '#u_name = :value',
    ExpressionAttributeNames: {
        '#u_name': 'name'
    },
    ExpressionAttributeValues: {
        ':value': 'Mykhaylo'
    },
};
docClient.scan(params, function(err, data) {
   ////////
});

or you need to create a gsi with name as hash key. Then you can use Query to get the result according to name

0
votes

You can run sql queries against dynamodb by doing the following:

  • open ubuntu bash
  • pip install dql
  • dql -r eu-west-2
  • scan * from table_name;