AWS DynamoDB Scan filterExpression - simple number comparison
Asked Answered
N

2

8

I am trying to do a simple dynamoDB scan with a filter expression (documentation here)

This is my expression string:

"attribute_exists("my_db_key") AND ("my_db_key" = 1)"

This simply states:

"If a value for my_db_key exists AND my_db_key EQUALS 1, return it in the results"

However it does not work and I get a this error:

Invalid FilterExpression: Syntax error; token: "1", near: "= 1)

I am aware that I can use an attribute value placeholder for values and then use that in the expression but I do not want to do this. And according to Amazon's documentation it is NOT required.

So how do I do this simple expression? Does anyone have an example or link to documentation? Amazon's documentation is unfortunately of no help.

NOTE: I am implementing this with AWSDynamoDBScanInput on iOS but my issue here is to do with global expression syntax so it should not matter.

Nahshun answered 4/3, 2015 at 15:4 Comment(0)
B
2

You have to use a placeholder and pass the value separately. Here's some documentation and a Post from AWS forums

Brasserie answered 4/3, 2015 at 18:11 Comment(0)
C
5

Your params need to look something like this (for the Node AWS library):

params = {
  "FilterExpression": 'attribute_exists("my_db_key") AND ("my_db_key" = :value)',
  "ExpressionAttributeValues": {
    ":value": 1
  },
  // ...
};

docClient.scan(params, function(err, data){
  // Handle err or process data
})

For some languages, the parameters should look more like this:

{
  "FilterExpression": 'attribute_exists("my_db_key") AND ("my_db_key" = :value)',
  "ExpressionAttributeValues": {
    ":value": {"N":1}
  },
  // ...
};
Cabdriver answered 4/2, 2016 at 19:35 Comment(4)
I'm trying to implement something similar but get a syntax error. Not sure what is wrong with it.Plagal
It should be "ExpressionAttributeValues":{":value":{"N":1}} (cc @KatharineOsborne)Ephedrine
You are correct. The Node library I'm using lets me exclude type identifiers.Cabdriver
The type identifiers are unnecessary when using the DocumentClient library.Lyrate
B
2

You have to use a placeholder and pass the value separately. Here's some documentation and a Post from AWS forums

Brasserie answered 4/3, 2015 at 18:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.