0
votes

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

1
Also provide ExpressionAttributeValues for sync value. - hoangdv
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 - John
':sync' : {BOOL: true} - hoangdv
Oh sorry. Now i get this error: UnexpectedParameter: Unexpected key 'KeyConditionExpression' found in params - John
Use FilterExpression instead. - hoangdv

1 Answers

1
votes

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){ ... }