16
votes

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
})
5

5 Answers

13
votes

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'
  }
}
7
votes

I had this issue and figured it out by using contains parameter

// Object stored in the DB looks like this: 
// [
//     'name' => 'myName',
//     'age' => '24',
//     'gender' => 'Male',
//     'visited' => [
//          'countries': ['Canada', 'USA', 'Japan', 'Australia'],
//          'last_trip': '2015/12/13',
//          'reviews_written': 20
//     ]
// 
// ];

$countries = ['Canada', 'USA', 'Japan', 'Australia'];

$paramToMatch = '24';

$client->query([
        'TableName'     => 'MyDyanmoDB',
        'KeyConditions' => [
            'age' => [
                'AttributeValueList' => [
                    $marshaler->marshalValue($paramToMatch)
                ],
                'ComparisonOperator' => 'EQ'
            ]
        ],
        'ExpressionAttributeNames' => [
            '#visited'   => 'visited',
            '#countries' => 'countries'
        ],
        'ExpressionAttributeValues' => [
            ':countries' => $marshaler->marshalValue($countries)
        ],
        'FilterExpression' => 'contains(:countries, #visited.#countries)',
]);
2
votes

Here's how I was able to use "scan" to get the items with a particular ID ("ContentID") in below example:

var params = {
    TableName: environment.ddbContentTableName,
    ProjectionExpression: "Title, ContentId, Link",
    FilterExpression: "ContentId in (:contentId1, :contentId2, :contentId3, :contentId4)",
    ExpressionAttributeValues: {":contentId1":102,":contentId2":104,":contentId3":103,":contentId4":101}
};

var docClient = new AWS.DynamoDB.DocumentClient();
docClient.scan(params, onQuery); 

I can then programmatically construct the FilterExpression and ExpressionAttributeValues based on known values e.g.

    // Create the FilterExpression and ExpressionAttributeValues
    var filterExpression =  "ContentId in ("; 

    var expressionAttributeValues = {};

    for (var i = 0; i < currentFavorites.length; i++) { 
        var contentIdName = ":contentId"+(i+1);
        
        if (i==0) {
            filterExpression = filterExpression + contentIdName;
        } else {
            filterExpression = filterExpression + ", " + contentIdName;
        }

        expressionAttributeValues[contentIdName] = currentFavorites[i];
    }

    filterExpression = filterExpression + ")";

    var params = {
        TableName: environment.ddbContentTableName,
        ProjectionExpression: "Title, ContentId, Link",
        FilterExpression: filterExpression,
        ExpressionAttributeValues: expressionAttributeValues
    };

    var docClient = new AWS.DynamoDB.DocumentClient();
    docClient.scan(params, onQuery); 
0
votes

I was also looking for a dynamic solution, than having to manually put each of the parameter names into the conditional expression. Below is a solution:

List<String> valList= new ArrayList<String>(); // Populate the Values in a List    
StringJoiner valMap =  new StringJoiner(","); // This will be the dynamic Value Map

int i=1;

        table = dynamoDB.getTable(myTable);
        StringBuilder filterExpression = new StringBuilder();
        Map<String, Object> eav = new HashMap<String, Object>();

        if(!valList.isEmpty())
        {
            for(String attrVal: valList)
             {
                eav.put(":val"+i, attrVal);
                valMap.add(":val"+i);
                i++;
            }   
            filterExpression.append("attrColName in ("+valMap.toString()+")"); //here attrColName is the DB attribute
        }

        ItemCollection<ScanOutcome> items;
            items = table.scan(
                filterExpression.toString(), //FilterExpression
                null, //ProjectionExpression - choose all columns
                null, //ExpressionAttributeNames - not used in this example
                eav);//ExpressionAttributeValues 
-1
votes
var params = {
TableName: "tableOne",
ProjectionExpression: "Title, ContentId, Link",
FilterExpression: "ContentId in (:contentIds)",
ExpressionAttributeValues: {":contentIds":[11,22,33,44,55]}
};

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