10110
10110

Reputation: 2665

Get all values present on a table for a given attribute without duplicates on a DynamoDB

Let's say I have this table:

Role        Alias       GrantedBy ...
Admin       xyz         xxx
Admin       abc         fff
Trainer     efg         kfk
Manager     lmn         jfj
Trainer     ijk         iui
Misc.       opq         jdj

Is there a way to get API Gateway to return something like ['Admin', 'Trainer','Manager', 'Misc.'] using a Mapping Template? If this is not possible, I can think of multiple ways of manipulating the results of a query to get the result I want but, what would be the best approach?

For example, would you recommend using a GSI that could help me accomplish this task?

Approach #1

Get all results in the DB and then reducing the Role attribute values into to an array of unique values,

I would consider this to be very bad in terms of performance and efficiency.

Approach #2

Create a GSI or configure the table so I can query in a way that returns all values available in the Role attribute.

So I would get something like:

Role
Admin
Admin
Trainer
Manager
Trainer
Misc.

And then I could iterate through that array and get the unique values.

Significantly better as I would be fetching just role values and then I would de-dupe them.

Approach #3

Configure the table so I can do something like a SELECT DISTINCT ROLES FROM TABLE_XYZ

Maybe declaring the projection expression declaration and just specify Role?

This would be ideal but I'm not sure if this is even possible in DynamoDB.

Optimal solution?

I'm wondering if maybe I'm trying to get DynamoDB to do something that it is not designed to do...

I come from SQL and am learning NoSQL and its pretty difficult to change how my brain is wired.

I appreciate your comments and suggestions.

Upvotes: 0

Views: 319

Answers (1)

jellycsc
jellycsc

Reputation: 12259

Well, here is my view on this problem. In the meantime, any suggestions and feedbacks are welcomed :)

Approach #1: Under no circumstances should you use it unless this is just a one-off case.

Approach #2: IMO, this is the go-to solution with the minimum change and extra effort required.

Approach #3: Not sure what you mean. Could you clarify how you are gonna do it?

=====================

Bonus Approach #4 for you: Maintain a list of unique roles.

This is very common in NoSQL. Some people call it pre-aggregation. You can think of it as something similar to a running SUM/AVG/COUNT etc.

Every time you need to add a new record, check its value of the role attribute against the list of unique roles that you maintain first. If the role doesn't exist yet, add it to the list and then proceed with the normal procedure.

Upvotes: 1

Related Questions