Amazon DynamoDB query for items whose key contains a substring

I am using the Amazon DynamoDB database and I have a list of items with various rows as a key. I want to request elements whose key contains a substring. For example, if some of the keys are:

"Abcd_aaa"
"Abcd_bbb"
"Abcd_ccc"

I want to query where the key contains "abcd" and these 3 elements will be returned. Is it possible?

+15
amazon amazon-web-services amazon-dynamodb
source share
2 answers

You can only query hashKey using the equality operator ( EQ ). If it is said that these values ​​("abcd_aaa", "abcd_bbb", "abcd_ccc") belong to your hashKey , then you must provide them completely. On the other hand, the Query operation allows partial matching on rangeKey with the possibility of several additional comparison operators:

 EQ | LE | LT | GE | GT | BEGINS_WITH | BETWEEN 

See the Query documentation for more information.

One possibility is to use hashKey and rangeKey , where the first part of your code will be hashKey and the last rangeKey , example:

 hashKey : abcd rangeKey : aaa 

By doing this with a hashKey (abcd) request, you will get all three records sorted by rangeKey

+18
source share

Scan will work

something like that

 var params = { TableName: "TABLE", ScanFilter: { "id": { ComparisonOperator: "CONTAINS", AttributeValueList: ["abcd"] } } }; var template = null; ddb.scan(params, function (err, data) { if (err) { console.error("Unable to query. Error:", JSON.stringify(err, null, 2)); } else { //console.log("Query succeeded."); data.Items.forEach(function (item) { console.log(item); }); } }); 
+3
source share

All Articles