Query on non-key attribute

2019-04-20 06:09发布

问题:

It appears that dynamodb's query method must include the partition key as part of the filter. How can a query be performed if you do not know the partition key?

For example, you have a User table with the attribute userid set as the partition key. Now we want to look up a user by their phone number. Is it possible to perform the query without the partition key? Using the scan method, this goal can be achieved, but at the expense of pulling every item from the table before the filter is applied, as far as I know.

回答1:

You'll need to set up a global secondary index (GSI), using your phoneNumber column as the index hash key.

You can create a GSI by calling UpdateTable.

Once you create the index, you'll be able to call Query with your IndexName, to pull user records based on the phone number.