Multiple FilterExpression in dynamodb scan

23,897

Solution 1

Probably I am late to answer. But, was searching for a similar scenario and got nothing. Still answering if someone else could benefit.

aws dynamodb scan --table-name test --select "COUNT" \
--filter-expression "score = :s OR score = :s1" \
--expression-attribute-values '{ ":s": { "N": "1" }, ":s1": { "N": "40" } }' \
--limit 100

Solution 2

Filter expressions are a kind of condition expression. You can combine filter expressions with boolean logic. However, in your example, you can get away without using AND/OR operators to combine expressions.

aws dynamodb scan --table-name test --select "COUNT" \
--filter-expression "score IN :s, :s1" \
--expression-attribute-values '{ ":s": { "N": "1" }, ":s1": { "N": "40" } }' \
--limit 100

Solution 3

For those who are searching for the nodeJs filter Expression:

const params = {
        TableName: "orderMessages",
        Key: {
            order_id,
            
        },
        FilterExpression: "#order_id = :ordrId OR #timestamp < :ts",
        ExpressionAttributeNames: {
            "#order_id": "order_id",
            "#timestamp": "timestamp"
          },
          ExpressionAttributeValues: {
            ":ordrId": order_id,
            ":ts": now
          },
    }

Happy coding :)

Solution 4

using between

aws dynamodb scan --table-name test --select "COUNT"  \
--filter-expression "(score between :s and :s1)"  \
--expression-attribute-values '{ ":s": { "N": "0" }, ":s1": { "N": "10" } }'
Share:
23,897
Albert s
Author by

Albert s

Updated on July 24, 2021

Comments

  • Albert s
    Albert s almost 3 years

    Im trying to build a histogram of a certain attribute in my dynamodb.
    I thought the easiest way would be to use multiple filter-expression
    This is my baseline query with a single filter-expression and it works

    aws dynamodb scan --table-name test --select "COUNT" --filter-expression "contains(score, :s)" --expression-attribute-values '{ ":s": { "N": "1" } }' --limit 100
    

    Now, im trying to extend it to multiple filter expressions and im not sure how.
    I have tried:

    aws dynamodb scan --table-name test --select "COUNT" --filter-expression "score = :s" --filter-expression "score = :s1" --expression-attribute-values '{ ":s": { "N": "1" }, ":s1": { "N": "40" } }' --limit 100
    

    and

    aws dynamodb scan --table-name test --select "COUNT" --filter-expression "score = :s" | "score = :s1" --expression-attribute-values '{ ":s": { "N": "1" }, ":s1": { "N": "40" } }' --limit 100
    
  • Albert s
    Albert s about 7 years
    This is what is returned after running the query as presented: An error occurred (ValidationException) when calling the Scan operation: Invalid FilterExpression: Syntax error; token: ":s", near: "IN :s,"
  • pmiranda
    pmiranda over 3 years
    How could you write the attribute in that --filter-expression is the name had : on it? For instance, instead using score use custom:attribute?
  • ps2goat
    ps2goat about 2 years
    needs parentheses? score IN (:s, :s1)