简体   繁体   English

在特定日期之间查询 DynamoDB -NodeJs

[英]Query DynamoDB between certain dates -NodeJs

I'm trying to query Dynamo DB for person_id between two dates.我正在尝试在 Dynamo DB 中查询两个日期之间的 person_id。 I have errors with the following code:我对以下代码有错误:

Please note that the primary key of the table is (event_id), but I'm not sure how/where to use it?请注意,表的主键是 (event_id),但我不确定如何/在哪里使用它?

 var params = { TableName: "tableName", IndexName: "person_id-event_date-index", FilterExpression: "#person_id =:person_id", KeyConditionExpression: "#person_id =:person_id and #event_date BETWEEN:from AND:to", ExpressionAttributeNames: { "#person_id": "person_id", "#event_date": "event_date" }, ExpressionAttributeValues: { ":person_id": person_id, ":from": start_date, ":to": end_date } } documentClient.query(params, function(err, data) { if (err) { console.error("Unable to query. Error:", JSON.stringify(err, null, 2)); callback(new Error(JSON.stringify(err))) } else { callback(null, data); } });

Can anyone please check how to make this working?任何人都可以请检查如何使这个工作?

Many thanks非常感谢

Short answer, you cannot do that unless the Table has some GSI (Global Secondary Indexes) set up that allow for that sort of query.简短的回答是,除非表设置了一些允许此类查询的GSI (全局二级索引),否则您不能这样做。

When doing a query you must pass a primary key value, you cannot do a query otherwise.进行查询时必须传递主键值,否则不能进行查询。

Considering that you want to query based on a different column, not the primary key, you need to see if you have an index defined with考虑到您要基于不同的列而不是主键进行查询,您需要查看是否有定义的索引

  • PrimaryKey = person_id and PrimaryKey = person_id 和
  • RangeKey = event_date RangeKey = event_date

If such an index is note defined you may rely on scan , but that's not recommended.如果注释定义了这样的索引,您可能会依赖scan ,但不建议这样做。

Here is the fix code:这是修复代码:

 var params = { TableName: "tableName", IndexName: "person_id-event_date-index", KeyConditionExpression: "#person_id =:person_id and #event_date BETWEEN:from AND:to", ExpressionAttributeNames: { "#person_id": "person_id", "#event_date": "event_date" }, ExpressionAttributeValues: { ":person_id": person_id, ":from": start_date, ":to": end_date } } documentClient.query(params, function(err, data) { if (err) { console.error("Unable to query. Error:", JSON.stringify(err, null, 2)); callback(new Error(JSON.stringify(err))) } else { callback(null, data); } }); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM