Reputation: 5003
i used amazon dynamoDb for storing my data. i created database like this
Partition key : userid , Type : String
Sort key : score , Type : Number
and i inserted some rows.
manaf1 | 100
manaf2 | 500
manaf1 | 200
manaf1 | 600
i need to get max & min score for selected user. This is my code
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.");
}
});
But i got error when running this code.
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
}
i need output like
manaf1 minimum score = 100
manaf1 maximum score = 600
Upvotes: 4
Views: 10291
Reputation: 39206
Please change the ExpressionAttributeValues as mentioned below. This should resolve the error.
ExpressionAttributeValues : {
':userid' : 'manaf1'
}
To sort the score in descending order, you can set the ScanIndexForward to 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
To sort the score in ascending order, you can set the ScanIndexForward to true.
Using ScanIndexForward you can get the MIN and MAX score record as first record in the result.
Upvotes: 6