我有dynamoDB表, 表名xx 主分区键ID(数字) 主排序键名称(String) 我想通过名字查询它. 'use strict';const AWS = require("aws-sdk");const dynamodb = new AWS.DynamoDB();const docClient = new AWS.DynamoDB.DocumentClient();e
表名xx
主分区键ID(数字)
主排序键名称(String)
我想通过名字查询它.
'use strict'; const AWS = require("aws-sdk"); const dynamodb = new AWS.DynamoDB(); const docClient = new AWS.DynamoDB.DocumentClient(); exports.handler = function(event, ctx, callback) { var params = { TableName: 'xx', KeyConditionExpression: "#name = :name", ExpressionAttributeNames:{ "#name": "name" }, ExpressionAttributeValues: { ":name":event.name } }; docClient.query(params, function(err, data){ if(err){ callback(err, null); }else{ callback(null, data); } }); }
但我得到一个错误称:“查询条件错过了关键架构元素:id:”如何处理?
DynamoDB是NoSQL数据库,因此您只能默认查询主键.你有几个选择:>创建全局二级索引并对其进行查询(Link):
“A global secondary index contains a selection of attributes from the base table, but they are organized by a primary key that is different from that of the table. The index key does not need to have any of the key attributes from the table; it doesn’t even need to have the same key schema as a table.”
>扫描而不是查询:
var params= { TableName:'xx', ProjectionExpression:'name', // remove this string if you want to get not only 'name' FilterExpression:'name = :name', ExpressionAttributeValues:{ ":name" : event.name } }; docClient.scan(params, function(err, data){ if(err){ callback(err, null); }else{ callback(null, data); } });
如果您的表格不会很大,那么扫描是更容易和更便宜的方式(我认为全球中学指数有相关成本),但“正确”的方法是使用全球二级指数.