Shibon
Shibon

Reputation: 1574

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
 }

Upvotes: 2

Views: 17736

Answers (2)

Tomasz Czechowski
Tomasz Czechowski

Reputation: 641

This works for me:

const paramsVehicles = {
    TableName: "vehicles",
    FilterExpression: "#manufacturer = :manufacturer and #modelName = :modelName",
    ExpressionAttributeNames: {
        "#manufacturer": "manufacturer",
        "#modelName": "modelName",
    },
    ExpressionAttributeValues: {
        ":manufacturer": "SEAT",
        ":modelName": "Ateca",
    }
};

I used a different table and model but the approach is generic and can be used in the asked example.

Upvotes: 0

Josh
Josh

Reputation: 1647

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.

Upvotes: 6

Related Questions