-1

I have an object in a table like the following

{
  key: "apple",
  owners: { "Adam": "some info",
             "Eve": "some info",
             "Samuel": "some info"}

}

I want to be able to fetch the first five owners of the item with the key "apple".

I've tried everything but the maximum I can do is fetch the entire owners map from the item with the given key from DynamoDB then take the first five.

Now with time if the owners map contains a million or more owners I will need a lot of read units to complete the fetch for just the top five owners which I am trying to avoid.

Any help on the above would be appreciated.

  • Yeah that's how dynamo and nosql db in general work. You shouldn't use it if it doesn't fit your access patterns. With that said you should explode the map out into multiplr kry value pairs so you can grab only the ones required instead of nesting them all in one key – Asad Awadia Sep 14 '22 at 18:15
  • @AsadAwadia Is there no other solution for this? I mean such a scaleable db without the support to iterate a nested map? – N.A.Shashank Sep 14 '22 at 18:22
  • 1
    Nope. Scalable doesn't mean queryable. If you want query support use a sql db (postgres, yugabyte, cockroach) – Asad Awadia Sep 14 '22 at 18:32
  • 1
    Not only does scalable not mean queryable, they're effectively opposites here, you have to trade-off one for the other, or at least that's what DDB has done. A bit like security vs. usability. – Richard Dunn Sep 14 '22 at 18:39
  • Also, DDB records are only 400KB in size, so you're only gonna get a few thousand "owners" in each record, at best. Owners need to be in another table. That'd apply to most DBs to be honest. – Richard Dunn Sep 14 '22 at 18:41
  • And... JSON objects are not ordered, so "first five" doesn't make sense. - Just pointing these things out for your own sake. – Richard Dunn Sep 14 '22 at 18:44
  • Richard is 100% on point here – Asad Awadia Sep 14 '22 at 19:21
  • This schema design is inappropriate for DynamoDB in most cases, and if you're actually going to have unbounded owners then you'll need to break them into separate items under the same item collection. You'd be best served by reviewing DynamoDB basics and coming up with a more typical design. – hunterhacker Sep 14 '22 at 19:30

1 Answers1

0

The short answer is its not possible to do what you are trying, you must return the entire map.

Second, your read request unit will charge you for the entire read of the item, regardless which fields are returned.

Depending on your application needs, you may benefit from remodeling your schema: enter image description here

Now you can simply Query with PK=apple & Limit=5

dynamodb.query({
    "TableName": 'Fruits',
    "KeyConditionExpression": `#pk = :val`,
    "ExpressionAttributeNames": {
        "#pk": "PK"
    },
    "ExpressionAttributeValues": {
        ":val": { "S": "apple" }
    },
    "Limit": 5
})
Leeroy Hannigan
  • 11,409
  • 3
  • 14
  • 31