1

I m using node typescript and local dynamodb. I'm unable to get a specific nested document. I m trying to get a post on the basis of id. I m using email as pk.Here is the dynamodb table

let params1:any= {
      TableName: "userTable",
      // Key: {
      //   email: event.body.email

      // },
      FilterExpression: "#posts[0].#id = :postId",
       // KeyConditionExpression: `#postId= :postId`,
      ExpressionAttributeNames: {
        "#posts": "posts",
        "#id": "id",

      },
      ExpressionAttributeValues: {
        ":postId": event.body.id

      },

     

    }

1 Answers1

1

As per comments.

you need to remodel your table design by using composite key structure.

composite key = email = hash key, postId =  range/sort key

note:- if you are using post-operation to query DB, event.body.id should be req.body.id.

you can simply use dynamodb get operation to grab single record

const getRecordParams = {
    TableName: tableName,
    Key: {
      email: req.body.id,    // hash key
      postId: req.body.postId  //range key
    },
  };
const dynamoDbGetResults = await dynamoDb
      .get(getRecordParams)
      .promise();

dynamoDbGetResults will have your entire record from which you can extract your desired values.

Note:- if you have constraints in changing table design an alternate solution would be to create index based on postId. Using that you can query directly on index using postId. dynamoDb supports local secondary index and Global secondary Index. they both have difference in terms of pricing, finding differences between them priorly for your use case would be good. https://stackoverflow.com/a/21383613/13126651

Jatin Mehrotra
  • 9,286
  • 4
  • 28
  • 67
  • Email is pk and i want to get specific nested doc suppose user has email as pk and posts a nested array of object i want to get a specific post of user based on email(pk) and post id. One way is to get user by email (pk) then using loop over posts array of object to find a specific post but i think this is not good way what if user has 3000+ posts then iterating and find a specific post within post array.... – shahid abbas Dec 28 '21 at 08:14
  • nested doc is not a problem. Create a table with composite key with email as range key and postid as sort key. combination of ( email,postid) will be a unique record. hence you can find a particular record of that particular user. ofcourse looping over array would be inefficient insteas remodel your table structure by using composite keys and then find a record wityh unique postid and particular email adress – Jatin Mehrotra Dec 28 '21 at 08:16
  • The logic would remain same, use get operation where `key would be {email:xyz,posId:unique}`, does it make sense? – Jatin Mehrotra Dec 28 '21 at 08:20
  • 1
    Yeah this also good idea. Email as hash key and postId as range key.. – shahid abbas Dec 28 '21 at 08:22
  • i have updated my answer as per the comments. if it helped you accept it as an answer so that it can help others in the future – Jatin Mehrotra Dec 28 '21 at 08:25