1

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?

1 Answers1

0
  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
Jason Wadsworth
  • 8,059
  • 19
  • 32
  • The student document is only an example. How can I use a filter to get the results? – stackaccount Feb 04 '20 at 00:56
  • filtering is just about useless...you still pay for reading all the records, then the ones that don't match the filter are thrown out and only the matching ones are returned. The only benefit is a bit less network use. – Charles Feb 04 '20 at 15:51
  • Thanks Jason and Charles. What is the best approach here that I can follow? – stackaccount Feb 04 '20 at 18:11