demoo
demoo

Reputation: 101

DynamoDb querying objects based on nested attribute

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?

Upvotes: 0

Views: 901

Answers (1)

Leeroy Hannigan
Leeroy Hannigan

Reputation: 19793

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

Upvotes: 1

Related Questions