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

asked 2 years ago377 views
1 Answer
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
EXPERT
answered 2 years ago
profile pictureAWS
EXPERT
Chris_G
reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions