eisklat
eisklat

Reputation: 51

DynamoDB access to items inside a dictionary

I am starting using dynamoDB with python, and I have a doubt I can't find an answer online. I have a JSON with this format:

{
"version": "1.0.0",
"trips":[
       {
        "paymentMode": "ONLINE",
        "type":"A",
        "locations":{
            "name": "Zona A",
            "payment":1000
            "company": ["XXX", "YYY"]

        },
        
       {
        "paymentMode": "CASH",
        "type":"B",
        "locations":{
            "name": "Zona D",
            "payment":200
            "company": ["XXX", "YYY"]
        }
    ]
} 

I can store it like that directly but I don't know if there is a way I can access individually each of the elements in trips in a get_item operation or similar? Like, having for example paymentMode and type as primary and sort key, even if they are inside the field "trips".

The input will be a Json like this, but I would like to put those fields as PK (considering they are unique) so I can retrieve only one item.

I know you can just scan everything and then iterate trips, but maybe with more elements, it will take too long. Is there a way to do this directly? if so, how can I do it? does dynamoDb do this automatically or i have to name it like trips.PaymentMode or something like that?

Thank you very much

Upvotes: 1

Views: 994

Answers (1)

tituszban
tituszban

Reputation: 5152

When designing how to structure your data in a database, you need to consider A) what data you are storing B) how you will be accessing your data.

Additionally, you need to consider the characteristics of the database in questions. For DynamoDB, the main constraint is your primary key. The primary key is either the partition key or the combination of partition key and the sort key, and it must be unique for all items.

Although it's not entirely clear, based on your JSON your items are trips with fields paymentMode, type and locations. If those are your fields on the item, what should be your key? You mention using paymentMode and type, however, these likely won't be suitable, as they probably won't be unique. If you have a time associated with these, a primary key of paymentMode_type and sort key of time might do the job for you, but that depends on the volume of the data.

What you might be better off doing, is assign a unique identifier to each item (e.g. a uuid) and add secondary indices over the fields you want to use to query (paymentMode, type). This will allow you to query efficiently without having to scan the entire database. Do however keep in mind, that you will incur additional costs for the secondary indices.

Upvotes: 1

Related Questions