Monday, June 3, 2024
51
rated 0 times [  52] [ 1]  / answers: 1 / hits: 30076  / 9 Years ago, wed, may 13, 2015, 12:00:00

I am having a hard time finding a useful example for a scan with FilterExpression on a DynamoDB table. I am using the javascript SDK in the browser.



I would like to scan my table and return only those records that have HASH field UID values within an array I pass to the Scan



Lets say I have an array of unique ids that are the hash field of my table
I would like to query these records from my DynamoDB table.



Something like below



var idsToSearch=['123','456','789'] //array of the HASH values I would like to retrieve
var tableToSearch = new AWS.DynamoDB();
var scanParams = {
TableName:myAwsTable,
AttributesToGet:['ID','COMMENTS','DATE'],
FilterExpression:'ID' in +idsToSearch+

}
tableToSearch.scan(scanParams), function(err,data){
if (err) console.log(err, err.stack); //error handler
else console.log(data); //success response
})

More From » amazon-web-services

 Answers
64

You should make use of the IN operator. It is also easier to use Placeholders for attribute names and attribute values. I would, however, advise against using a Scan in this case. It sounds like you already have the hash key attribute values that you want to find, so it would make more sense to use BatchGetItem.



Anyways, here is how you would do it in Java:



ScanSpec scanSpec = new ScanSpec()
.withFilterExpression(#idname in (:val1, :val2, :val3))
.withNameMap(ImmutableMap.of(#idname, ID))
.withValueMap(ImmutableMap.of(:val1, 123, :val2, 456, :val23, 789));
ItemCollection<ScanOutcome> = table.scan(scanSpec);


I would imagine using the Javascript SDK it would be something like this:



var scanParams = {
TableName:myAwsTable,
AttributesToGet: ['ID','COMMENTS','DATE'],
FilterExpression: '#idname in (:val1, :val2, :val3)',
ExpressionAttributeNames: {
'#idname': 'ID'
},
ExpressionAttributeValues: {
':val1': '123',
':val2': '456',
':val3': '789'
}
}

[#66623] Monday, May 11, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
randall

Total Points: 492
Total Questions: 99
Total Answers: 103

Location: Solomon Islands
Member since Fri, Oct 8, 2021
3 Years ago
randall questions
Wed, Mar 16, 22, 00:00, 2 Years ago
Tue, Nov 10, 20, 00:00, 4 Years ago
Sat, Oct 31, 20, 00:00, 4 Years ago
;