代码之家  ›  专栏  ›  技术社区  ›  Marek Urbanowicz user7125929

Dynamoose-无法使用2个键查询和where

  •  1
  • Marek Urbanowicz user7125929  · 技术社区  · 6 年前

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

    总结:

    ValidationException: Query key condition not supported 我需要在给定位置的最后(数量)秒内查找记录。 One another 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
        }));
    
    执行函数的代码

    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();
      }
    
    2 回复  |  直到 6 年前
        1
  •  5
  •   gerrytan    6 年前

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

        2
  •  0
  •   Charlie Fish    6 年前

    我敢肯定问题是当你 rangeKey: true createdAt id

    locationId 索引如下所示:

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

    这样,您就非常明确地知道要设置哪个索引 作为 rangeKey 为了。

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

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