如何使用 Nodejs 在没有主排序键的情况下在 DynamoDB 中进行扫描

How to scan in DynamoDB without primary sort key with Nodejs

我正在尝试使用 Nodejs 在没有主排序键的情况下在 DynamoDB 中进行扫描。 我有以下 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
};

我放了两个对象:

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

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

如何扫描所有 barname = 'john' 和 sync = true 的对象?

我试过类似的东西:

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

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

扫描未发现任何内容:

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

但我有 2 个匹配的对象:

您收到的错误是因为您使用的是 DynamoDB.DocumentClient,但您在过滤器表达式中指定了完整的属性类型。如果您只是将表达式更改为不包含它应该工作的类型。我不会深入探讨为什么这可能是个坏主意(扫描)。

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

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