Reputation: 383
I am using an Amazon DynamoDB database, and I have a list of items with various strings as the key. I want to query for items whose key contains a substring. For example, if some of the keys are:
"abcd_aaa"
"abcd_bbb"
"abcd_ccc"
I want to query where a key contains "abcd" and these 3 items will be returned. Is this possible?
Upvotes: 38
Views: 49581
Reputation: 449
Scan will work
something like this
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);
});
}
});
Upvotes: -5
Reputation: 5143
You can only query the hashKey
using the equality operator (EQ
). That being said if those values ("abcd_aaa", "abcd_bbb", "abcd_ccc") belong to your hashKey
then you have to provide them entirely. On the other hand, the Query
operation does allow partial matching on the rangeKey
with the option of a few additional comparison operators:
EQ | LE | LT | GE | GT | BEGINS_WITH | BETWEEN
See the Query
documentation for more details.
One possibility would be to use a hashKey and rangeKey
where the first part of your code would be the hashKey
and the last the rangeKey
, example:
hashKey : abcd
rangeKey : aaa
By doing this when you query by hashKey
(abcd), you would receive all three records sorted by the rangeKey
Upvotes: 39