node.js 查询条件缺少关键架构元素:验证错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47681108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Query condition missed key schema element : Validation Error
提问by Suhail Gupta
I am trying to query dynamodbusing the following code:
我正在尝试dynamodb使用以下代码进行查询:
const AWS = require('aws-sdk');
let dynamo = new AWS.DynamoDB.DocumentClient({
service: new AWS.DynamoDB(
{
apiVersion: "2012-08-10",
region: "us-east-1"
}),
convertEmptyValues: true
});
dynamo.query({
TableName: "Jobs",
KeyConditionExpression: 'sstatus = :st',
ExpressionAttributeValues: {
':st': 'processing'
}
}, (err, resp) => {
console.log(err, resp);
});
When I run this, I get an error saying:
当我运行它时,我收到一条错误消息:
ValidationException: Query condition missed key schema element: id
I do not understand this. I have defined idas the partition key for the jobstable and need to find all the jobs that are in processingstatus.
我不明白。我已定义id为jobs表的分区键,需要查找所有处于processing状态的作业。
回答by smcstewart
You're trying to run a query using a condition that does not include the primary key. This is how queries work in DynamoDB. You would need to do a scan for the info in your case, however, I don't think that is the best option.
您正在尝试使用不包含主键的条件运行查询。这就是查询在 DynamoDB 中的工作方式。您需要扫描您的案例中的信息,但是,我认为这不是最佳选择。
I think you want to set up a global secondary indexand use that to query for the processingstatus.
我认为您想设置一个全局二级索引并使用它来查询processing状态。
回答by Ula
In another answer @smcstewartresponded to this question. But he provides a link instead of commenting why this error occurs. I want to add a brief comment hoping it will save your time.
在另一个答案中,@smcstewart回答了这个问题。但他提供了一个链接,而不是评论为什么会发生此错误。我想添加一个简短的评论,希望它能节省您的时间。
AWS docs on Querying a Tablestates that you can do WHEREcondition queries (e.g. SQL query SELECT * FROM Music WHERE Artist='No One You Know') in the DynamoDB way, but with one important caveat:
关于查询表的 AWS 文档指出,您可以以 DynamoDB 方式进行WHERE条件查询(例如SQL query SELECT * FROM Music WHERE Artist='No One You Know'),但有一个重要的警告:
You MUST specify an EQUALITY condition for the PARTITION key, and you can optionally provide another condition for the SORT key.
您必须为 PARTITION 键指定一个 EQUALITY 条件,并且您可以选择为 SORT 键提供另一个条件。
Meaning you can only use key attributes with Query. Doing it in any other way would mean that DynamoDB would run a full scanfor you which is NOT efficient- less efficient than using Global secondary indexes.
这意味着您只能将关键属性与Query. 以任何其他方式执行此操作将意味着 DynamoDB 将为您运行完整扫描,这并不高效- 比使用全局二级索引效率低。
So if you need to query on non-key attributes using Query is usually NOT an option - best option is using Global Secondary Indexesas suggested by @smcstewart.
因此,如果您需要使用 Query 查询非键属性通常不是一个选项 - 最好的选择是使用@smcstewart建议的全局二级索引。
I found this guideto be useful to create a Global secondary index manually.
我发现本指南对于手动创建全局二级索引很有用。
If you need to add it using CloudFormation here is a relevant page.
如果您需要使用 CloudFormation 添加它,这里是一个相关页面。
回答by dfloresdev
i solved the problem using AWS.DynamoDB.DocumentClient() with scan, for sample (nodejs):
我使用 AWS.DynamoDB.DocumentClient() 和扫描解决了这个问题,示例(nodejs):
var docClient = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: "product",
FilterExpression: "#cg = :data",
ExpressionAttributeNames: {
"#cg": "categoria",
},
ExpressionAttributeValues: {
":data": category,
}
};
docClient.scan(params, onScan);
function onScan(err, data) {
if (err) {
// for the log in server
console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));
res.json(err);
} else {
console.log("Scan succeeded.");
res.json(data);
}
}

