简体   繁体   English

Dynamoose - 无法查询和2键的位置

[英]Dynamoose - cannot query and where with 2 keys

I have opened a related issue on GitHub, but maybe someone here will be able to help quicker. 我已经在GitHub上打开了一个相关问题,但也许这里的某个人可以更快地帮忙。

Summary: 摘要:

ValidationException: Query key condition not supported I need to find records in last (amount) seconds on a given location. ValidationException: Query key condition not supported我需要在给定位置的最后(金额)秒内查找记录。 Pretty simple, but already related to other issues: One and another one 很简单,但已经与其他问题有关: 一个另一个

WORKS : 工作

Activity.query('locationId').eq(locationId).exec();

DOES NOT WORK : 不工作

Activity.query('locationId').eq(locationId).where('createdAt').ge(date).exec();

Code sample: 代码示例:

Schema 架构
async getActivitiesInLastSeconds(locationId: string, timeoutInSeconds: number) {
    const Activity = schema.Activity(this.dynamoose);
    const date = moment().subtract(timeoutInSeconds, 'seconds').valueOf();
    return await Activity.query('locationId').eq(locationId)
      .where('createdAt').ge(date).exec();
  }
Code which executes the function 执行该功能的代码

Funny part is that I found this as workaround proposed to be used until they merge PR giving ability to specify timestamps as keys, but unfortunately it does not work. 有趣的是,我发现这是建议使用的解决方法,直到它们合并PR给出将时间戳指定为键的能力,但不幸的是它不起作用。

 async getActivitiesInLastSeconds(locationId: string, timeoutInSeconds: number) { const Activity = schema.Activity(this.dynamoose); const date = moment().subtract(timeoutInSeconds, 'seconds').valueOf(); return await Activity.query('locationId').eq(locationId) .where('createdAt').ge(date).exec(); } 

I suspect createdAt is not a range key of your table / index. 我怀疑createdAt不是表/索引的范围键。 You need to either do .filter('createdAt').ge(date) or modify your table / index schema. 您需要执行.filter('createdAt').ge(date)或修改表/索引架构。

I'm pretty sure the problem is that when you specifying rangeKey: true on the createdAt property you are telling that to be used on the global index (I don't think that is the correct term). 我敢肯定的问题是,当你指定rangeKey: truecreatedAt属性,告诉将要对全球指数(我不认为这是正确的术语)使用。 That range key will be linked to the id property. 该范围键将链接到id属性。

I believe the easiest solution would be to change your locationId index to be something like the following: 我相信最简单的解决方案是将locationId索引更改为如下所示:

index: {
    global: true,
    rangeKey: 'createdAt',
},

That way you are being very explicit about which index you want to set createdAt as the rangeKey for. 这样,你正在你要设置哪些指标非常明确createdAt作为rangeKey的。

After making that change please remember to sync your changes with either your local DynamoDB server or the actual DynamoDB service, so that the changes get populated both in your code and on the database system. 进行更改后,请记住将更改与本地DynamoDB服务器或实际的DynamoDB服务同步,以便在代码和数据库系统中填充更改。

Hopefully this helps! 希望这有帮助! If it doesn't fix your problem please feel free to comment and I'll help you further. 如果它无法解决您的问题,请随时发表评论,我会进一步帮助您。

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

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