5

how to specify multiple conditions on DynamoDB Query Operation?

var params = {
            TableName: "liveTraining",
            KeyConditionExpression: "#ContentId = :contentId and #CompanyId = :companyId and #Status=:status",
            ExpressionAttributeNames:{
                  "#ContentId": "ContentId",
                  "#CompanyId": "CompanyId",
                  "#Status": "Status"
                },
            ExpressionAttributeValues: {
                ":contentId": contentId,
                ":companyId": companyId,
                ":status": 'Active'
                }
        };
        return new Promise((resolve, reject) => {
          dynamoDB.query(params, function (error, response) {
            if (error) {
              console.log('Unable to fetch data', JSON.stringify(error, null, 2));
            } else {
              console.log('fetched data', response);
            }
          })
        });

when I specified more than two conditions using AND operator I got an error:

Unable to fetch data {
  "message": "Conditions can be of length 1 or 2 only",
  "code": "ValidationException",
  "time": "2019-04-12T06:53:34.766Z",
  "requestId": "MOE9FDN6I62NLROVPPTDMS5V2JVV4KQNSO5AEMVJF66Q9ASUAAJG",
  "statusCode": 400,
  "retryable": false,
  "retryDelay": 29.17802723976769
}
  • Possible duplicate of: https://stackoverflow.com/questions/21515765/retrieve-all-items-from-dynamodb-using-query – Boney Apr 12 '19 at 08:34
  • 3
    The key condition expression can only contain conditions on the hash key and sort key. Any other conditions must go in a filter expression. – Matthew Pope Apr 12 '19 at 12:10

1 Answers1

-1

You can specify at max two conditions for a query operation within the KeyConditionExpression. Any further conditions need to applied through a filter using the FilterExpression.

Every query operation requires at least a Partition Key. You can optionally specify a Sort Key.

The KeyConditionExpression allows for the following:

  1. an equals operator on the Partition Key
  2. an accepted operator / operation on the Sort Key

Here's the list of comparison operators you can use for Sort Keys.

Any other additional attributes must be specified in a FilterExpression. Keep in mind, you will be charged the RCUs for everything picked up by the KeyConditionExpresion, despite only receiving what has not been filtered by the FilterExpression.

From the same link as above, regarding consumption and filters on queries:

A filter expression is applied after a Query finishes, but before the results are returned. Therefore, a Query consumes the same amount of read capacity, regardless of whether a filter expression is present.

AWS DynamoDB Query docs on query mechanics sums it up nicely:

You must provide the name of the partition key attribute and a single value for that attribute ... Use the KeyConditionExpression parameter to provide a specific value for the partition key ... You can optionally narrow the scope of the Query operation by specifying a sort key value and a comparison operator in KeyConditionExpression. To further refine the Query results, you can optionally provide a FilterExpression.

bd_
  • 96
  • 1
  • 4
  • Your response was informative, but I think the meat of this question is "how to specify multiple conditions" since the docs are vague on the syntax. – Carter Apr 17 '23 at 21:28
  • The error occurred when the asker tried to use more than two conditions, which is what I addressed. I will add additional information around specifying more than one condition. – bd_ Apr 18 '23 at 22:41