Gabriel Cunha Gabriel Cunha - 4 months ago 290
Node.js Question

Why DynamoDB scan with Limit and FilterExpression not return the items that match the filter requirements?

I need make a scan with limit and a condition on DynamoDB.

The docs says:


In a response, DynamoDB returns all the matching results within the scope of the Limit value. For example, if you issue a Query or a Scan request with a Limit value of 6 and without a filter expression, DynamoDB returns the first six items in the table that match the specified key conditions in the request (or just the first six items in the case of a Scan with no filter). If you also supply a FilterExpression value, DynamoDB will return the items in the first six that also match the filter requirements (the number of results returned will be less than or equal to 6).



The code (NODEJS):

var params = {
ExpressionAttributeNames: {"#user": "User"},
ExpressionAttributeValues: {":user": parseInt(user.id)},
FilterExpression: "#user = :user and attribute_not_exists(Removed)",
Limit: 2,
TableName: "XXXX"
};

DynamoDB.scan(params, function(err, data) {
if (err) {
dataToSend.message = "Unable to query. Error: " + err.message;
} else if (data.Items.length == 0) {
dataToSend.message = "No results were found.";
} else {
dataToSend.data = data.Items;
console.log(dataToSend);
}
});




Table XXXX definitions:


  • Primary partition key: User (Number)

  • Primary sort key: Identifier (String)

  • INDEX:


    • Index Name: RemovedIndex

    • Type: GSI

    • Partition key: Removed (Number)

    • Sort key: -

    • Attributes: ALL





In code above, if I remove the
Limit
parameter, DynamoDB will return the items that match the filter requirements. So, the conditions are ok. But when I scan with
Limit
parameter, the result is empty.

The XXXX table, has 5 items. Only the 2 firsts have the
Removed
attribute. When I scan without
Limit
parameter, DynamoDB returns the 3 items without
Removed
attribute.

What i'm doing wrong?

Answer

From the docs that you quoted:

If you also supply a FilterExpression value, DynamoDB will return the items in the first six that also match the filter requirements

By combining Limit and FilterExpression you have told DynamoDB to only look at the first two items in the table, and evaluate the FilterExpression against those items. Limit in DynamoDB can be confusing because it works differently from limit in a SQL expression in a RDBMS.

Comments