turtle
turtle

Reputation: 8073

How can I query DynamoDB and return all child values for a specific key in an array

I have a table in DynamoDB where each row has the following structure:

{
     "id": 1,
     "data": [
         {
             "key": "A",
             "value": "a"
         },
         {
             "key": "B",
             "value": "b"
         }
     ]
 },
 ...

I would like to create a query that returns a single row of data where the "value" keys have been filtered out of the results. Essentially, I am looking for a way to produce this output:

{
     "id": 1,
     "data": [
         {
             "key": "A"
         },
         {
             "key": "B"
         }
     ]
}

The trick is that the "data" list contains an unknown number of elements. Using the following code with Projection Expressions, I can get close to what I need in NodeJS:

var AWS = require('aws-sdk');

var docClient = new AWS.DynamoDB.DocumentClient();

var params = { 
    TableName: "MyTable",
    Key: {
        "id": 1
    },
    ProjectionExpression:"id, data[0].key"
};

docClient.get(params, function(err, data) {
    if (err)
        console.log(JSON.stringify(err, null, 2));
    else
        console.log(JSON.stringify(data, null, 2));
});

The code above returns the following data:

{
     "id": 1,
     "data": [
         {
             "key": "A"
         }
     ]
}

How can I get DynamoDB to return all the keys inside data and not just one?

Upvotes: 0

Views: 2140

Answers (1)

ketan vijayvargiya
ketan vijayvargiya

Reputation: 5649

This isn't supported.

As I see it, you have two options:

  • If there's an upper cap on number of entries in data, for e.g., 100, then you can modify your ProjectionExpression to something like: "id, data[0].key, data[1].key, data[2].key, data[3].key, ..., data[100].key". It should work, because as per the DynamoDB documentation, there is only one constraint on list indexes:

The index in a list dereference must be a non-negative integer

  • If there is no such upper cap, filter out on client-side.

Upvotes: 1

Related Questions