0

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?

0

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I've read here docs.aws.amazon.com/amazondynamodb/latest/developerguide/… that I can have max 5 secondary indexes for table. But what if all my columns possibly could become required? For example, table with fields: id, name, email, city, country, gender, subscription. And I'd like to use all of them and make requests to find user by any of this fields
@Grynets You cannot create more than 5 gsi. You can always use scan and filter (as I described in the answer) to get data using any field. The only problem with scan is that it will be very slow for large dataset. So if you have a large dataset and facing performance problem with scan, then you might have to create a separate table to create gsi on more fields.
0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.