Scan Function in DynamoDB with reserved keyword as FilterExpression NodeJS

37,066

Solution 1

Solved :

There are two parameters taken by aws-sdk :

Expression Attribute Name

Expression Attribute Value

both provide the functionality of replacing placeholders used in the attributes list. Here by Attributes it is a bit ambiguous, where I got confused. The wizards over at aws mean both the key and value when they use the term attribute.

So in a case where you want to use a reserved key word as a key attribute use the Expression Attribute Name parameter with #(pound) to denote the placeholder.

Similarly where you want to use placeholders for value attribute use the Expression Attribute Value parameter with :(colon) to denote the placeholder.

So finally my code (working) looks like this :

var param = {
  TableName: "faasos_orders",
  FilterExpression: "#order_status = :delivered OR #order_status = :void OR #order_status = :bad",
  ExpressionAttributeValues: {
    ":delivered": "delivered",
    ":void": "void",
    ":bad": "bad"
  },
  ExpressionAttributeNames: {
    "#order_status": "status"
  }
};  
  dynamodb.scan(param, function (err, data) {....});

Solution 2

:status is a placeholder in your expression that you aren't providing a value for. See how you are providing values for your other placeholders here:

expressionAttr[":delivered"] = "delivered";
expressionAttr[":bad"] = "bad";
expressionAttr[":void"] = "void"

You need to do the same for the :status placeholder. I don't see anything about a reserved word in the error message, so I'm not sure why you think that's the cause of the error. The error very specifically states that you aren't providing a value for the :status placeholder.

Share:
37,066
Saleem Ahmed
Author by

Saleem Ahmed

Are you working with high dimensional pixel/textual data ? Do your new year resolutions include finding the perfect experienced deep learning guy ? Do you on a regular basis implement computer vision and nlp papers in pytorch or tensorflow ? Are you looking for someone to brainstorm the mathematics behind back propagating the gradient from an affine transformation layer ? Are you actively looking for a person to rapidly prototype your cuda deep learning models ? Do you want the person on your team to explicitly understand broadcasting tensors to fit inception layers ? Do you routinely wonder if your positional embedding could do with a better attention mechanism ? Are you kept up at night calculating the output dimensions of your 100+ convolution layers ? And after you get through all of this you want a scalable full stack deployment at a production level with an awesome documentation and intricate unit tests ? If you answered 'Yes' to 3 or more of the above questions. Do ping me. Even if you're not hiring and just want to hash out a problem you're stuck on. If you are hiring, then even more so ! Cheers. Let's build some disentangled representations while we are at it.

Updated on July 09, 2022

Comments

  • Saleem Ahmed
    Saleem Ahmed almost 2 years

    My scan function :

    var tableName = 'faasos_orders',
        filterExp = 'status = :delivered OR status = :void OR status = :bad',
        projectionValues = '',
        expressionAttr = {};    
        expressionAttr[":delivered"] = "delivered";
        expressionAttr[":bad"] = "bad";
        expressionAttr[":void"] = "void"; 
        limit = 10;
      dynamoConnector.getItemUsingScan(tableName, filterExp, projectionValues, expressionAttr, function (err, data) {  ...........} 
    

    Error on running :

        { [ValidationException: Invalid FilterExpression: Attribute name is a reserved keyword; reserved keyword: status]
      message: 'Invalid FilterExpression: Attribute name is a reserved keyword; reserved keyword: status',
      code: 'ValidationException',
      time: Mon Apr 18 2016 21:57:30 GMT+0530 (IST),
      requestId: 'AV6QFHM7SPQT1QR3D4OO81ED4FVV4KQNSO5AEMVJF66Q9ASUAAJG',
      statusCode: 400,
      retryable: false,
      retryDelay: 0 }
    

    Now I do get the point I am trying to use a reserved keyword in th e filterExpression which is illegal. But if I run the same function through aws gui it returns data beautifully (check image for details): Scan function on status through gui

    So the question is how do I add the filter expression through node without having to change the key name ???

  • Saleem Ahmed
    Saleem Ahmed about 8 years
    Please see the edits . had been toying with the placeholder as explained here docs.aws.amazon.com/amazondynamodb/latest/developerguide/… Though neither #status nor :status seems to work , { [ValidationException: ExpressionAttributeValues contains invalid key: Syntax error; key: "#status"] And with :status the expression though runs smoothly but returns no data
  • Mark B
    Mark B about 8 years
    You have to use the placeholder syntax like in your original question text, but then you also need to provide the value for that placeholder: expressionAttr[":status"] = "status"
  • Saleem Ahmed
    Saleem Ahmed about 8 years
    That doesnt match the condition mate. No value is returned. Though I have values in the db as shown on image. The aws doc says to use # for expression attribute names and : for expression attribute values.
  • Mark B
    Mark B about 8 years
    Then use # and see if it works? The fact is you have to provide a value for any placeholders you put in the query, which is what the error message is complaining about.
  • kometen
    kometen about 8 years
    Thank you for your effort. Had a similar error. :-)
  • Saleem Ahmed
    Saleem Ahmed about 8 years
    Yay. Amazons Documentation is just so awesome you know. Though found this their main guide for DynamoDB : Dev Guide Latest Amazon dynamodb @kometen
  • Elliot Blackburn
    Elliot Blackburn over 4 years
    This also works in C# (with a slight syntax change) and presumably all other languages supported by the aws sdks.
  • Craig.C
    Craig.C over 3 years
    Here is the c# syntax @ElliotBlackburn mentioned docs.aws.amazon.com/sdk-for-net/v2/developer-guide/…
  • Steve S.
    Steve S. over 2 years
    Old post but it seems to need some clarification. The error message specifically mention an error about a reserved keyword! "Status" is a reserved keyword per documentation (docs.aws.amazon.com/amazondynamodb/latest/developerguide/…) . That's why you needed to use the ExpressionAttributeNames to be still be able to use the attribute "status" in the filter as below.