Gilad Novik
Gilad Novik

Reputation: 4614

DynamoDB query by secondary index only

I store user accounts in DynamoDB:

{
  email: '[email protected]',
  expires: 1548807053247,
}

My hash key is the email field.

I want to add a daily cron job which will send a reminder email for all accounts about to expire (in the next 14 days).

For that, I need to query on expires field alone - without using the hash key.

I assume I need to define a secondary index on this field (probably global and not local?), but I'm not sure on how to write the proper query for it.

I'm using AWS.DynamoDB.DocumentClient for accessing the table, thanks in advance!

Upvotes: 2

Views: 1634

Answers (2)

Adrian Praja
Adrian Praja

Reputation: 412

Im having the same problem very often

what I do is , add another attribute named "all" with value of 1
(you can use any key/value )
and then create a GSI

  • partition: all
  • sort: expires

to optimise a bit you could add this attribute only to active users

or you could add an attribute active Number and use it as partition key for the GSI

this is a very inefficient partition key distribution
since all items belong to one partition but I found no other way around

I'd be happy to hear another solution

Upvotes: 1

Daniel Vassallo
Daniel Vassallo

Reputation: 344561

Just specify the IndexName in addition to the TableName when you call the Query API. (Docs.) The rest is the same as if you were querying the table.

Upvotes: 3

Related Questions