我尝试从DynamoDB获取哪些userLimit介于较低和较高值之间。我使用JSON将较低的值发送给我的代码。我的Node.js代码:
var AWS = require('aws-sdk');
var db = new AWS.DynamoDB();
exports.handler = function(event, context) {
var low=event.ke1;
var params = {
TableName: "Events", //"StreamsLambdaTable",
ProjectionExpression: "userLimit, description", //specifies the attributes you want in the scan result.
FilterExpression: "userLimit between :lower and :higher",
// ExpressionAttributeNames: {
// "#yr": "year",
// },
ExpressionAttributeValues: {
":lower": {"N": "low"},
":higher": {"N": "50"}
}
};
db.scan(params, function(err, data) {
if (err) {
console.log(err); // an error occurred
}
else {
data.Items.forEach(function(record) {
console.log(
record.description.S + "");
});
context.succeed(data.Items);
// context.done(null,{"Result": "Operation succeeded."});
//res.send(data.name);
}
// return next();
});
};
我收到错误:
[ValidationException: ExpressionAttributeValues contains invalid value: The parameter cannot be converted to a numeric value: low for key :lower]
message: 'ExpressionAttributeValues contains invalid value: The parameter cannot be converted to a numeric value: low for key :lower',
如果我将":lower": {"N": "low"},
更改为":lower": {"N": low},
,则错误为:
{ [ValidationException: ExpressionAttributeValues contains invalid value: Supplied AttributeValue is empty, must contain exactly one of the supported datatypes for key :lower]
message: 'ExpressionAttributeValues contains invalid value: Supplied AttributeValue is empty, must contain exactly one of the supported datatypes for key :lower',
我的JSON数据是:
{
"key1": "1"
}
如何使用key1的值设置较低的值?
编辑:
有错误:
var low=event.ke1;
舒尔德
var low=event.key1;
编辑2:
运行该代码时发生了另一个错误:
var params = {
TableName: "Events", //"StreamsLambdaTable",
ProjectionExpression: "userLimit, description, type", //specifies the attributes you want in the scan result.
KeyConditionExpression: "type = :tempType",
// ExpressionAttributeNames: {
// "#yr": "year",
// },
ExpressionAttributeValues: {
":tempType": {"S": "Party"}
}
};
错误:
{ [UnexpectedParameter: Unexpected key 'KeyConditionExpression' found in params]
message: 'Unexpected key \'KeyConditionExpression\' found in params',
编辑3:
我目前的代码是:
var AWS = require('aws-sdk');
var db = new AWS.DynamoDB();
exports.handler = function(event, context) {
var low=event.key1;
var params = {
TableName: "Events", //"StreamsLambdaTable",
ProjectionExpression: "ID, userLimit, description, #tp", //specifies the attributes you want in the scan result.
KeyConditionExpression: "#tp = :tempType",
ExpressionAttributeNames: {
"#tp": "type",
},
ExpressionAttributeValues: {
":tempType": {"S": "Party"}
}
};
db.query(params, function(err, data) {
if (err) {
console.log(err); // an error occurred
}
else {
data.Items.forEach(function(record) {
console.log(
record.description.S + "");
});
context.succeed(data.Items);
// context.done(null,{"Result": "Operation succeeded."});
//res.send(data.name);
}
// return next();
});
};
但是我收到了一个错误:
{ [ValidationException: Query condition missed key schema element: ID]
message: 'Query condition missed key schema element: ID',
答案 0 :(得分:2)
" KeyConditionExpression"不适用于scan
操作。
如果属性是idexed字段,则需要更改要查询的操作;如果必须是扫描操作,则需要使用FilterExpression
。
答案 1 :(得分:-1)
根据您的哈希键和索引,您可以使用:
DynamoDB查询:
var params = {
TableName: 'table_name',
KeyConditions: { // indexed attributes to query
// must include the hash key value of the table or index
// with 'EQ' operator
attribute_name: {
ComparisonOperator: 'EQ', // (EQ | NE | IN | LE | LT | GE | GT | BETWEEN |
// NOT_NULL | NULL | CONTAINS | NOT_CONTAINS | BEGINS_WITH)
AttributeValueList: [ { S: 'STRING_VALUE' }, ],
},
// more key conditions ...
}
};
dynamodb.query(params, function(err, data) {
if (err) console.log(err); // an error occurred
else console.log(data); // successful response
});
或DynamoDB扫描:
var params = {
TableName: 'table_name',
Limit: 0, // optional (limit the number of items to evaluate)
ScanFilter: { // optional (map of attribute name to Condition)
attribute_name: {
ComparisonOperator: 'EQ', // (EQ | NE | IN | LE | LT | GE | GT | BETWEEN |
// NOT_NULL | NULL | CONTAINS | NOT_CONTAINS | BEGINS_WITH)
AttributeValueList: [ { S: 'STRING_VALUE' }, ],
},
// more conditions ...
}
};
dynamodb.scan(params, function(err, data) {
if (err) console.log(err); // an error occurred
else console.log(data); // successful response
});