-1

I have a DynamoDB table like this:enter image description here

I want to list all posts irrespective of users, i-e by getting all data whose sort key is "post". How can I achieve this?

I have heard about Global Secondary Index, but couldn't figure out how to use them.

Maurice
  • 11,482
  • 2
  • 25
  • 45
plutolaser
  • 448
  • 4
  • 17
  • Maybe this will help? https://stackoverflow.com/questions/9297326/is-it-possible-to-order-results-with-query-or-scan-in-dynamodb/36883510#36883510 – kometen Oct 14 '21 at 06:53
  • We cannot query without specifying partition key (PK in this case). Here's the error on executing the code:`{ "message": "Query condition missed key schema element: PK", "code": "ValidationException", "time": "2021-10-14T07:06:39.851Z", "statusCode": 400, "retryable": false, }` – plutolaser Oct 14 '21 at 07:07
  • I see. It's been long time since I worked on dynamodb. But as I recall data is partitioned by PK. So you might need to have another table where you partition by the sort key. – kometen Oct 14 '21 at 08:07
  • Yeah, I have to use GSI for this. But couldn't figure out how. – plutolaser Oct 14 '21 at 08:30

1 Answers1

3

You create a global secondary index with a Key Schema like this:

  • Partition Key: SK attribute of the base table
  • Sort Key: PK attribute of the base table

It's called an inverted index. Then you can Query the Global Secondary Index by specifying the IndexName in the Query and search for all items that have "post" as the value for SK.

Maurice
  • 11,482
  • 2
  • 25
  • 45