Home Articles

DynamoDB Scan FilterExpression返回空结果

Asked
Viewed 475 times
1

我试图使用nodejs aws sdk过滤由dynamodb扫描操作返回的数据,但返回的数据有0项 .

Response : {"Items":[],"Count":0,"ScannedCount":15}

我尝试过使用FilterExpression和ScanFilter但得到了相同的结果 .

FilterExpression:

var params = {
            TableName: tableName,
            FilterExpression: 'active = :active',
            ExpressionAttributeValues: {
                ':active': {
                    S: '1'
                }
            }
        };

ScanFilter:

var params = {
            TableName: tableName,
            ScanFilter: {
                'active': {
                    "AttributeValueList": [{ "S": "1" }],
                    "ComparisonOperator": "EQ"
                }
            }
        };

这是nodejs代码:

dynamodb.scan(params, onScan);

        function onScan(err, data) {
            if (err) {
                console.error('Unable to scan the table. Error JSON:', JSON.stringify(err, null, 2));
            } else {
                if (typeof data.LastEvaluatedKey != 'undefined') {
                    params.ExclusiveStartKey = data.LastEvaluatedKey;
                    dynamodb.scan(params, onScan);
                }
                if (data && data.Items)
                    callback(data.Items);
                else
                    callback(null);
            }
        }

我在dynamodb控制台中检查了相同的过滤条件并获得了预期的结果 .

dynamodb console screenshot

3 Answers

  • 4

    @bharathp回答为我做了诀窍,

    在aws命令行中必须将参数指定为 {"S":"s1"}

    aws dynamodb scan \
     --table-name aws-nodejs-typescript-dev \
     --filter-expression "id = :search" \
     --expression-attribute-values '{":search":{"S":"s1"}}'
    

    在节点代码中 ":search": "s1"

    const params = {
      TableName: process.env.DYNAMODB_TABLE,
      ExpressionAttributeValues: {
        ":search": "s1"
      },
      FilterExpression: "id = :search",
      ProjectionExpression: "id",
    };
    
    dynamoDb.scan(params, (error, result) => {
    
  • 1

    在AWS CLI上,这对我来说很合适,

    aws dynamodb扫描

    --table-name myTableName
    --filter-expression“email =:a”
    --expression-attribute-values'{“:a”:{“S”:“xxxxx@xxxxx.com”}}'

    但是,当尝试在NodeJs / Angular5 / typescript上编写相同的查询时,它会失败:

    private getDynamoEmail(email: string): Promise<ScanOutput> {
        const params: any = {
         TableName: this.config.database.myTableHere,
         KeyConditionExpression "email = :e" 
         ExpressionAttributeValues: {
          ":email": {
            S: email, // Had to remove this part to work and use 
                      // ExpressionAttributeNames instead
           },
         },
         FilterExpression: ":e = :email",
        };
        return this.docClient.scan(params).promise();
    }
    

    为了工作,我不得不省略属性类型,所以这里的代码工作:

    private getDynamoEmail(email: string): Promise<ScanOutput> {
        // tslint:disable-next-line:no-any
        const params: any = {
         ExpressionAttributeNames: {"#email": "email"},
         ExpressionAttributeValues: {":email": email},
         FilterExpression: "#email = :email",
         TableName: this.config.database.registeredLiveAccounts,
        };
        return this.docClient.scan(params).promise();
    }
    

    希望帮助丹尼尔

  • 0

    它表示扫描了15个项目,未找到任何项目 . 您可能会在 if (typeof data.LastEvaluatedKey != 'undefined') { params.ExclusiveStartKey = data.LastEvaluatedKey; dynamodb.scan(params, onScan); } 的循环中找到该项目

    扫描操作扫描所有项目并过滤匹配 .

Related