如何根据多个(一个主要)属性从 DynamoDB 获取项目 Lambda/NodeJS

How To Get Item From DynamoDB Based On Multiple (one primary) Attributes Lambda/NodeJS

我在 DynamoDB 中的 table 结构如下所示:

uuid(主键)|知识产权 |用户代理

我想从 lambda 内的 NodeJS 函数中获取其 ip 和 useragent 与我提供的信息匹配的项目的 uuid。

随着时间的推移,

Scan 的效率越来越低,而且随着时间的推移,每周都会有数百万的项目被添加到 table。

这是我用来尝试完成此操作的代码:

function tieDown(sIP, uA){
 const userQuery = {
       Key : {
         "ip" : "192.168.0.1",
         "userAgent" : "sample"
       },
       TableName: "mytable"
      };
  return ddb.get(userQuery, function(err, data){
   if (err) console.log(err.stack);
  }).promise();
}

当这段代码执行时,抛出以下错误ValidationException: The provided key element does not match the schema

所以我想我的问题是:

谢谢!

如果没有指定分区键和排序键(如果有),则无法使用 get 操作获取单个项目。大多数情况下应避免扫描。您可能需要的是一个 Global Secondary Index,它允许您通过 ipuserAgent query。请记住,不能保证 GSI 上的记录是唯一的,因此您可能会得到不止一个结果。