I am using the AWS DynamoDB DocumentClient to query my DynamoDB tables. In my tables I have a property called "_id" that holds a unique ID for each entry. When I try to query for a record based on this ID, I end up receiving an error that says: "Invalid KeyConditionExpression: Syntax error; token: \"_\", near: \"_id\"". The code that makes this query is below.
function findById(id) {
//Build query
var params = {};
params.TableName = "recordDev";
params.IndexName = "_id";
params.KeyConditionExpression = '_id = :id';
params.ExpressionAttributeValues = {
':id': id
};
return DynamoDB
.query(params).promise()
.then(function(records) {
return records.Items[0];
})
.catch(function(error) {
return Promise.reject(error);
});
};
Is there something that I am missing with regards to using an "_" when building the query params for DynamoDB? I have tried looking around for similar errors, but have been unable to find any that are like my scenario.
Each primary key attribute must be a scalar (meaning that it can hold only a single value). The only data types allowed for primary key attributes are string, number, or binary. There are no such restrictions for other, non-key attributes.
Yes, like all the other database management systems, DynamoDB also supports all the conditional operators, User can specify a condition that is satisfied for a put, update, or delete operation to work on an item.
The following are the naming rules for DynamoDB: All names must be encoded using UTF-8, and are case-sensitive. Table names and index names must be between 3 and 255 characters long, and can contain only the following characters: a-z.
DynamoDB supports two types of primary keys, a Hash Key and a Hash and Range Key. A Hash Key consists of a single attribute that uniquely identifies an item. A Hash and Range Key consists of two attributes that together, uniquely identify an item.
Can you try using the ExpressionAttributeNames param?
var params = {};
params.TableName = "recordDev";
params.IndexName = "_id";
params.KeyConditionExpression = '#id = :id';
params.ExpressionAttributeValues = {
':id': id
};
params.ExpressionAttributeNames: {
"#id":"_id"
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With