Andrew Goulart Andrew Goulart - 20 days ago 5
Javascript Question

Unable to Access DynamoDB Item's Map attribute values with ExpressionFilter using Javascript

I have the following item in my DynamoDB table:

{
"quid" : {
"S":"87CEE4F1-5458-4A65-A3F5-1837A353B14D"
},
"objectType" : {
"S":"event"
},
"document" : {
"M" : {
"externalID" : {
"S" : "12-34-567890"
},
"eventFormat" : {
"N" : "0"
}
}
}
}


I'm trying to access that item with the following javascript code

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

AWS.config.update({
region: "us-west-2"
});
AWS.config.loadFromPath('./config.json');

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

var params = {
TableName : "events",
FilterExpression : "document.eventFormat = :nForm",
ExpressionAttributeValues : {":nForm":{"N":"0"}}
}

docClient.scan(params, function(err, data){
if (err) console.log(err);
else console.log(data);
})


When I remove the FilterExpression and ExpressionAttributesValues, I get a full scan of my table successfully, and get to see all items contained within. When I add the FilterExpression and the ExpressionAttributesValues, I don't get any results back.

Any help figuring out how to filter my scan by either the externalID or the eventFormat would be greatly appreciated!

Answer

Please try with the below params. The data type is not required in ExpressionAttributeValues for scalar numeric values. This worked as expected for me.

var params = {
        TableName: "events",    
        FilterExpression: "document.eventFormat = :nForm",
        ExpressionAttributeValues: {
            ":nForm": 0
        }
    };