node.js - How can I query DynamoDB and return all child values for a specific key in an array -
i have table in dynamodb each row has following structure:
{ "id": 1, "data": [ { "key": "a", "value": "a" }, { "key": "b", "value": "b" } ] }, ...
i create query returns single row of data "value" keys have been filtered out of results. essentially, looking way produce output:
{ "id": 1, "data": [ { "key": "a" }, { "key": "b" } ] }
the trick "data" list contains unknown number of elements. using following code projection expressions, can close 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 following data:
{ "id": 1, "data": [ { "key": "a" } ] }
how can dynamodb return keys inside data
, not one?
this isn't supported.
as see it, have 2 options:
- if there's upper cap on number of entries in
data
, e.g., 100, can modify projectionexpression like:"id, data[0].key, data[1].key, data[2].key, data[3].key, ..., data[100].key"
. should work, because per dynamodb documentation, there 1 constraint on list indexes:
the index in list dereference must non-negative integer
- if there no such upper cap, filter out on client-side.
Comments
Post a Comment