0

I'm trying to scan in DynamoDB without primary sort key with Nodejs. I have the following Table

var params = {
    AttributeDefinitions: [
        {
        AttributeName: "barname",
        AttributeType: "S"
        },
        {
        AttributeName: "timestamp",
        AttributeType: "S"
        }
    ],
    KeySchema: [
        {
        AttributeName: "barname",
        KeyType: "HASH"
        },
        {
        AttributeName: "timestamp",
        KeyType: "RANGE"
        }
    ],
    ProvisionedThroughput: {
        ReadCapacityUnits: 1,
        WriteCapacityUnits: 1
    },
    TableName: tableName
};

I put with two objects:

data1 = {
    barname: "paul",
    timestamp: new Date().toISOString(),
    sync: false,
    number : "1234",
}

data2 = {
    barname: "john",
    timestamp: new Date().toISOString(),
    sync: true,
    number : "333",
}

How to scan all objects that barname = 'john' and sync = true ?

I tried something like that:

var params = {
    TableName: tableName,
    FilterExpression: "sync = :sync and barname = :barname",
    ExpressionAttributeValues: {
        ':barname' : {S: 'john'},
        ':sync' : {BOOL: true}
      },
  };

  dynamodb.scan(params, function(err, data){ ... }

The scan doesn't find anything:

{ Items: [], Count: 0, ScannedCount: 4 }

But i have 2 objects that match:

enter image description here

7
  • 1
    Also provide ExpressionAttributeValues for sync value. Commented Apr 13, 2021 at 1:00
  • I updated the code. I got the error: MultipleValidationErrors: There were 2 validation errors: * UnexpectedParameter: Unexpected key 'KeyConditionExpression' found in params * InvalidParameterType: Expected params.ExpressionAttributeValues[':sync'] to be a structure Commented Apr 13, 2021 at 1:04
  • 1
    ':sync' : {BOOL: true} Commented Apr 13, 2021 at 1:06
  • Oh sorry. Now i get this error: UnexpectedParameter: Unexpected key 'KeyConditionExpression' found in params Commented Apr 13, 2021 at 1:13
  • 1
    Use FilterExpression instead. Commented Apr 13, 2021 at 1:21

1 Answer 1

1

The error you are getting is because you are using the DynamoDB.DocumentClient, but you are specifying the full attribute type in the filter expression. If you just change the expression to not include the type it should work. I won't go into why this is probably a bad idea (scanning).

var params = {
    TableName: tableName,
    FilterExpression: "sync = :sync and barname = :barname",
    ExpressionAttributeValues: {
        ':barname' : 'john',
        ':sync' : true
      },
  };

  dynamodb.scan(params, function(err, data){ ... }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.