0

In my dynamoDB table I have two fields which I intend to query for a specific value. One will returns several results, the second will return one result (aka one row).

I already have a range key field which is the date that the raw inserted.

What are my options if I want to have two fields to query by a single field?

From my understanding, there is an option to create a secondary index, but I must query it alongside with the hash key, which is not what I need. Is there any option to achieve that in a single table instead of creating a second table for that query?

Example Folders table:

 user_id (hash key)    
 date_created (range key)    
 folder_id

First query: Select all folders Id's for a specific user order by date descending

Second query: Select single raw for a specific folder_id

If I use secondary index on the folder_id, I will have to query it alongside the user_id (hash key), which is as you can see, not what I want to achieve.

Thanks.

3 Answers 3

6

You need a Global Secondary Index.

folder_id (hash key)
date_created (range) - optional

Then you can also query by the folder_id

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

Comments

3

As you said yourself, you can't use LSI in this scenario.

You will have to create another table where folder_id is the hash key and the rest of the data as attributes.

Comments

1

You can also query for one primary key that will return the least amount of results than filter out the rest in code.

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.