Search code examples
node.jsamazon-web-servicesamazon-dynamodb

AWS DYNAMO DB query with multiple condition


I am trying to fetch details from dynamo db using the bellow query in this DOMAIN and SERVICE are just key(non primary key)

let params = {
    TableName: asset_table,
    ConsistentRead: true,
    ProjectionExpression: 'ID,SERVICE',
    KeyConditionExpression: "DOMAIN = :service_name AND SERVICE EQ :service_domain",
    ExpressionAttributeValues: {
         ":service_name": {"S":service },
         ":service_domain": {"S":domain}
    }
};
docClient.scan(params, (err, data) => {
    if (err) {
        onComplete(err);
    } else {
        console.log(data);
        onComplete(null, {
            data
        });
    }
});

this give error as given bellow

{
"message": "ExpressionAttributeValues can only be specified when using expressions: FilterExpression is null",
"code": "ValidationException",
"time": "2019-01-09T09:47:09.180Z",
"requestId": "0G3C02E6251S2H1IQ2LQUTN04JVV4KQNSO5AEMVJF66Q9ASUAAJG",
"statusCode": 400,
"retryable": false,
"retryDelay": 8.070453867451622
 }

Solution

  • The scan method does not accept a KeyConditionExpression, in params. Instead you must use the FilterExpression param.

    The error is telling you this as it can see your ExpressionAttributeValues but no FilterExpression to use them.

    According to AWS Docs - Working With Scans:

    The syntax for a filter expression is identical to that of a condition expression. Filter expressions can use the same comparators, functions, and logical operators as a condition expression. For more information, Condition Expressions.

    Checkout DDB Docs here for full details on how to use the scan method.