0

How can I query all the objects from a given table in Amazon DynamoDb using sub-object id? I will explain with example:

Orders table data:

{
 "id": "76ds6ds76",
 "publishDate": "2022-09-20",
 "returnDate": null,
 "book": {
  "bookId": "327a7cdfeececd",
  "name": "Hello world"
 }
},
{
 "id": "838ds990",
 "publishDate": "2022-09-30",
 "returnDate": null,
 "book": {
  "bookId": "327a7cdfeececd",
  "name": "Hello world"
 }
}

I want to get all the Orders with bookId == "327a7cdfeececd". Is there any possibility to query that?

1
  • Not really. That is not what DynamoDB is designed for. You cannot put indices onto nested attributes. Move bookId and name up into the top level and you can put an index onto it. stackoverflow.com/a/30441854/2442804 Commented Oct 17, 2022 at 9:30

1 Answer 1

1

The best way you can achieve that is by making bookid a top level attribute and creating a GSI based on it.

The other option you have is using a Scan and FilterExpression. This will read all the items in the table in order to find the one you need, while it would work well for small tables, it may not be advisable to do it with large tables due to poor performance and cost.

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Scan.html#Scan.FilterExpression

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Lee, I'll have to think about some refactor then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.