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

M U*_*M U 2 javascript nosql amazon-dynamodb dynamoose

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

概括:

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

作品

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
    }));
Run Code Online (Sandbox Code Playgroud) 执行函数的代码

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

ger*_*tan 5

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