我正在尝试使用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控制台中检查了相同的过滤条件并获得了预期的结果。
答案 0 :(得分:4)
@bharathp回答为我做了诀窍,
aws命令行中的必须将参数指定为 {" S":" s1"}
aws dynamodb scan \
--table-name aws-nodejs-typescript-dev \
--filter-expression "id = :search" \
--expression-attribute-values '{":search":{"S":"s1"}}'
节点代码中的只是":搜索":" s1"
const params = {
TableName: process.env.DYNAMODB_TABLE,
ExpressionAttributeValues: {
":search": "s1"
},
FilterExpression: "id = :search",
ProjectionExpression: "id",
};
dynamoDb.scan(params, (error, result) => {
答案 1 :(得分: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();
}
希望有帮助 丹尼尔
答案 2 :(得分:0)
表示扫描了15个项目,未找到任何项目。您可以在if (typeof data.LastEvaluatedKey != 'undefined') {
params.ExclusiveStartKey = data.LastEvaluatedKey;
dynamodb.scan(params, onScan);
}
当扫描操作扫描所有项目并过滤匹配时。