Reputation: 13
I'm writing a search function for a database that contains article titles, bodies, author names and dates. I want to query the database with a given title.
Scanning without the filter works fine, but the filtering just returns an empty result. I've tried filtering by every letter in the alphabet, including by the first letter of the title of the first entry in the database. Nothing.
Below is the Lambda code. I am getting the title I want to filter by from an API call parameter. I've checked, and that comes through fine.
const params = {
TableName: event.queryStringParameters.TableName,
ExpressionAttributeValues: {
":title": event.queryStringParameters.title
},
FilterExpression: "title = :title",
};
body = await dynamo.scan(params, function (err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
data.Items.forEach(function (element, index, array) {
console.log(
"printing",
element.title
);
});
}
}).promise();
Scan results:
{Items: Array(0), Count: 0, ScannedCount: 12}
I've spent hours on this and looked all over StackOverflow and though I can find similar problems, none of the solutions I saw worked for me.
Upvotes: 0
Views: 1229
Reputation: 23793
FilterExpression: "title = :title"
I've tried filtering by every letter in the alphabet
Equality would seem to be the wrong choice, do you perhaps want begins_with()?
Also when using scan or query, an empty result set should be expected. DDB will only read 1MB of data, then apply filters and return what's left...which may be nothing. You should be reading in a loop and applying the returned LastEvaluatedKey
as the ExclusiveStartKey
to the next read. If there's no LastEvaluatedKey
then there's no more data.
Finally, Scan()
is not an operation to use often. As it reads the entire table every time.
Upvotes: 3