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年前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ