stackaccount
stackaccount

Reputation: 79

Dynamodb Query/Scan nested documents. Specifying GSI

I have following structure of the DynamoDb document

{
 "studentName"(PK): "Ben",

 "studendId":  123,

 "UpdatedTimestamp"(SortKey): 1221432432

 "Subjects": [

      {
       SubjectName:  "Math"
       SubjectMarks:  80
       UpdatedTimeStamp:  234324324

     },
     {
     SubjectName: "History"
     SubjectMarks: 30
     UpdatedTimeStamp: 213234234
     }
]

}

I am currenty using the Dynamodb Mapper to save the documents.

I have the following questions

  1. Each time I update the table it creates a new record in DynamoDb table? Is this expected? While querying I have to set Limit to 1 each time to get the most recent record. Is it because I have the SortKey to be the current timestamp? Will it help if I remove the sortKey?

  2. I would like to perform the following query: Get all the StudentNames who have SubjectMarks > 60 This should return the student Ben but with only the Subject Math. Is this possible? Do I need to use query or scan? Will it help to use GSI? How can I use the dot operator in this case as the Subjects is a list?

Upvotes: 1

Views: 85

Answers (1)

Jason Wadsworth
Jason Wadsworth

Reputation: 8885

  1. Yes, this is expected, and yes, it is because you have the timestamp as your sort key. Keep in mind that a partition key is not the same as a primary key. You could say that the combination of partition key and sort key are effectively the primary key.
  2. This is a bit of a more complex question, and there are many ways to accomplish it. Ultimately you cannot use a complex/nested object as a key, meaning you can't query based on it. You can filter, but that isn't really what you're going to want to do. What you probably need to do here is take a step back and get a better understanding of NoSQL databases, and how access patterns are really the key to data modeling. There are a lot of great papers/blogs/videos on the subject. One of my favorite videos is this session at re:Invent 2018

Upvotes: 0

Related Questions