DynamoDB: Query Incorrect operand type
amazon-dynamodb, javascript
Solution
Actually, you have two errors here:
The first one is, indeed, that you are trying to apply an operator that is not `=` on your Primary partition key. HOWEVER, this is not the reason for the error message you get here!
In your error message, the `M` type is for `Mapping` (or `Dictionary`), on which the operator `>` cannot be applied. Indeed, it seams you are using the `DocumentClient` abstraction of the SDK, and as specified in the documentation:
The document client simplifies working with items in Amazon DynamoDB by abstracting away the notion of attribute values.
It means that when making calls using the document client, you should be having the following query parameters:
var params = {
TableName : document.getElementById("tableName").value,
KeyConditionExpression: "Project_ID = :v1", //I modified this to fix the first point
ExpressionAttributeValues: {
":v1": 0 //Not a dictionary!
}
};
instead of:
var params = {
TableName : document.getElementById("tableName").value,
KeyConditionExpression: "Project_ID = :v1", //I modified this to fix the first point
ExpressionAttributeValues: {
":v1": {"N": "0"} //This is an attribute value !
}
};
Problem
I'm attempting to read all values in a DynamoDB table above a certain value. I have the primary partition key set to a Number called Project_ID. I am running a query to see all values above a certain ID - mostly to test out functionality, however I am getting an error when running the code. The code: ``` var params = { TableName : document.getElementById("tableName").value, KeyConditionExpression: "Project_ID > :v1", "ExpressionAttributeValues": { ":v1": {"N": "0"} } }; docClient.query(params, function(err, data) { if (err) { document.getElementById('textarea').innerHTML += "Unable to query. Error: " + "\n" + JSON.stringify(err, undefined, 2); } else { data.Items.forEach(function(project) { //JSON.stringify(project); document.getElementById('textarea').innerHTML += "\n" + project.Project_Name + ": " + project.Project_Ref; }); } }); ``` The output ``` `Unable to query. Error: { "message": "Invalid KeyConditionExpression: Incorrect operand type for operator or function; operator or function: >, operand type: M", "code": "ValidationException", "time": "2017-04-28T10:52:31.381Z",` ```