Fetch first five items from nested map DynamoDB NodeJS

0

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. node.js amazon-dynamodb

已提問 2 年前檢視次數 389 次
1 個回答
3

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:

PKSKOther
appleOWNER#AdamData
appleOWNER#EveData
appleOWNER#SamuelData
appleOWNER#LeeData
orangeOWNER#DaveData
orangeOWNER#MatthewData
orangeOWNER#SarahData
orangeOWNER#BenData

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
})
profile pictureAWS
專家
已回答 2 年前
profile pictureAWS
專家
Chris_G
已審閱 2 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南