【问题标题】:Dynamoose - cannot query and where with 2 keysDynamoose - 不能用 2 个键查询和在哪里
【发布时间】:2018-09-22 10:16:57
【问题描述】:

我已经在 GitHub 上打开了一个相关问题,但也许这里的人能够更快地提供帮助。

总结:

ValidationException: Query key condition not supported 我需要在给定位置的最后(数量)秒内查找记录。 很简单,但已经涉及到其他问题: Oneanother one

作品

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

不工作

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

代码示例:

架构
const Activity = (dynamoose: typeof dynamooseType) => dynamoose.model<ActivityType, {}>('Activity',
    new Schema({
      id: {
        type: String,
        default: () => {
          return uuid();
        },
        hashKey: true,
      },
      userId: {
        type: String,
      },
      locationId: {
        type: String,
        rangeKey: true,
        index: {
          global: true,
        },
      },
      createdAt: { type: Number, rangeKey: true, required: true, default: Date.now },
      action: {
        type: Number,
      },
    }, {
      expires: 60 * 60 * 24 * 30 * 3, //  activity logs to expire after 3 months
    }));
执行函数的代码

有趣的是,我发现这是建议使用的解决方法,直到他们合并 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();
  }

【问题讨论】:

    标签: javascript nosql amazon-dynamodb dynamoose


    【解决方案1】:

    我怀疑createdAt 不是您的表/索引的范围键。您需要执行.filter('createdAt').ge(date) 或修改您的表/索引架构。

    【讨论】:

    • 请看code:createdAt: { type: Number, rangeKey: true, required: true, default: Date.now },是范围键。
    • @gerrytan 是完全正确的。我还提交了一个答案,其中提供了有关如何更改索引以将 createdAt 设置为范围键的更多详细信息。但是使用 filter 方法也应该可以。
    【解决方案2】:

    我很确定问题在于,当您在 createdAt 属性上指定 rangeKey: true 时,您是在告诉要在全局索引上使用它(我认为这不是正确的术语)。该范围键将链接到 id 属性。

    我相信最简单的解决方案是将您的 locationId 索引更改为如下所示:

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

    这样您就可以非常明确地将createdAt 设置为rangeKey 的索引。

    进行更改后,请记住将更改与本地 DynamoDB 服务器或实际 DynamoDB 服务同步,以便将更改填充到您的代码和数据库系统中。

    希望这会有所帮助!如果它不能解决您的问题,请随时发表评论,我会进一步帮助您。

    【讨论】:

    • 不知道我可以为索引指定范围键。明天会检查它(这是星期天:))并让你知道!
    • 嗨。它运行良好,但只有一个键......(但仍然非常感谢!)我正在努力进行查询:language: { type: String, index: { global: true, rangeKey: 'channel', }, }, channel: { type: String, rangeKey: true, index: { global: true, rangeKey: 'messageCode', }, }, messageCode: { type: String, rangeKey: true, },
    • TranslationsModel.query('language').eq('EN') .where('channel').eq(NotificationChannel.Email) .and() .where('messageCode').eq('TEST_MSG') .exec();
    • 我可以在哪里做通道.. 但是当我添加and().where('messageCode') it fails with Query 条件错过了关键架构元素` 和以前一样有什么好的建议吗?
    • @MU .where() 仅适用于范围键。尝试改用.filter()
    猜你喜欢
    • 2018-03-31
    • 2021-05-07
    • 2016-07-30
    • 2014-10-29
    • 2011-05-31
    • 1970-01-01
    • 1970-01-01
    • 2021-07-14
    • 2020-12-19
    相关资源
    最近更新 更多