使用 node.js sdk 从 dynamoDb 获取最小值和最大值
get min and max value from dynamoDb using node.js sdk
我使用亚马逊 dynamoDb 来存储我的数据。我这样创建了数据库
Partition key : userid , Type : String
Sort key : score , Type : Number
然后我插入了一些行。
manaf1 | 100
manaf2 | 500
manaf1 | 200
manaf1 | 600
我需要获取所选用户的最高和最低分数。这是我的代码
var AWS = require("aws-sdk");
AWS.config.update({
region: "myRegion"
});
var docClient = new AWS.DynamoDB.DocumentClient();
var params = {
"TableName": "tableName",
"KeyConditionExpression": "#userid = :userid ",
"ExpressionAttributeNames": {
"#userid": "userid"
},
"ExpressionAttributeValues": {
":userid": {
"S": "manaf1"
}
}
};
docClient.query(params, function (err, data) {
if (err) {
console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
} else {
console.log("Query succeeded.");
}
});
但是 运行 这段代码时出现错误。
Unable to query. Error: {
"message": "One or more parameter values were invalid: Condition parameter type does not match schema type",
"code": "ValidationException",
"time": "2016-08-31T06:12:13.825Z",
"requestId": "S1DFFJNDNVV4KQNSO5AEDASFMVJF66Q9ASHDFUAAJG",
"statusCode": 400,
"retryable": false,
"retryDelay": 0
}
我需要像
这样的输出
manaf1 minimum score = 100
manaf1 maximum score = 600
- 是否可以从 dynamoDb 获取数字字段的最大值或最小值?
- 与此代码相关的问题是什么?
请按如下所述更改 ExpressionAttributeValues。这应该可以解决错误。
ExpressionAttributeValues : {
':userid' : 'manaf1'
}
要按降序排列分数,您可以将 ScanIndexForward 设置为 false。
var params = {
TableName : table,
KeyConditionExpression : 'userid = :userid',
ExpressionAttributeValues : {
':userid' : 'manaf1'
},
ScanIndexForward: false
};
If ScanIndexForward is true, DynamoDB returns the results in the order
in which they are stored (by sort key value). This is the default
behavior. If ScanIndexForward is false, DynamoDB reads the results in
reverse order by sort key value, and then returns the results to the
client.
Type: Boolean
要按升序对分数进行排序,您可以将 ScanIndexForward 设置为 true。
使用ScanIndexForward,您可以获得 MIN 和 MAX 分数记录作为结果中的第一条记录。
我使用亚马逊 dynamoDb 来存储我的数据。我这样创建了数据库
Partition key : userid , Type : String
Sort key : score , Type : Number
然后我插入了一些行。
manaf1 | 100
manaf2 | 500
manaf1 | 200
manaf1 | 600
我需要获取所选用户的最高和最低分数。这是我的代码
var AWS = require("aws-sdk");
AWS.config.update({
region: "myRegion"
});
var docClient = new AWS.DynamoDB.DocumentClient();
var params = {
"TableName": "tableName",
"KeyConditionExpression": "#userid = :userid ",
"ExpressionAttributeNames": {
"#userid": "userid"
},
"ExpressionAttributeValues": {
":userid": {
"S": "manaf1"
}
}
};
docClient.query(params, function (err, data) {
if (err) {
console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
} else {
console.log("Query succeeded.");
}
});
但是 运行 这段代码时出现错误。
Unable to query. Error: {
"message": "One or more parameter values were invalid: Condition parameter type does not match schema type",
"code": "ValidationException",
"time": "2016-08-31T06:12:13.825Z",
"requestId": "S1DFFJNDNVV4KQNSO5AEDASFMVJF66Q9ASHDFUAAJG",
"statusCode": 400,
"retryable": false,
"retryDelay": 0
}
我需要像
这样的输出manaf1 minimum score = 100
manaf1 maximum score = 600
- 是否可以从 dynamoDb 获取数字字段的最大值或最小值?
- 与此代码相关的问题是什么?
请按如下所述更改 ExpressionAttributeValues。这应该可以解决错误。
ExpressionAttributeValues : {
':userid' : 'manaf1'
}
要按降序排列分数,您可以将 ScanIndexForward 设置为 false。
var params = {
TableName : table,
KeyConditionExpression : 'userid = :userid',
ExpressionAttributeValues : {
':userid' : 'manaf1'
},
ScanIndexForward: false
};
If ScanIndexForward is true, DynamoDB returns the results in the order in which they are stored (by sort key value). This is the default behavior. If ScanIndexForward is false, DynamoDB reads the results in reverse order by sort key value, and then returns the results to the client.
Type: Boolean
要按升序对分数进行排序,您可以将 ScanIndexForward 设置为 true。
使用ScanIndexForward,您可以获得 MIN 和 MAX 分数记录作为结果中的第一条记录。