Dynamoose - 无法查询和使用 2 个键的位置
Dynamoose - cannot query and where with 2 keys
我已经在 GitHub 上打开了一个相关问题,但也许这里有人能够更快地提供帮助。
总结:
ValidationException: Query key condition not supported
我需要在给定位置的最后(数量)秒内查找记录。
很简单,但已经涉及到其他问题:
One and 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
}));
执行函数的代码
有趣的是,我发现这是建议使用的变通方法,直到它们合并 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();
}
我怀疑 createdAt
不是您的 table / 索引的范围键。您需要执行 .filter('createdAt').ge(date)
或修改您的 table / 索引架构。
我很确定问题在于,当您在 createdAt
属性 上指定 rangeKey: true
时,您是在告诉它要用于全局索引(我不认为这是正确的术语)。该范围键将链接到 id
属性.
我认为最简单的解决方案是将您的 locationId
索引更改为如下所示:
index: {
global: true,
rangeKey: 'createdAt',
},
这样您就可以非常明确地说明要将 createdAt
设置为 rangeKey
的索引。
进行更改后,请记住将您的更改与本地 DynamoDB 服务器或实际的 DynamoDB 服务同步,以便在您的代码和数据库系统中填充更改。
希望这对您有所帮助!如果它不能解决您的问题,请随时发表评论,我会进一步帮助您。
我已经在 GitHub 上打开了一个相关问题,但也许这里有人能够更快地提供帮助。
总结:
ValidationException: Query key condition not supported
我需要在给定位置的最后(数量)秒内查找记录。
很简单,但已经涉及到其他问题:
One and 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
}));
执行函数的代码
有趣的是,我发现这是建议使用的变通方法,直到它们合并 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();
}
我怀疑 createdAt
不是您的 table / 索引的范围键。您需要执行 .filter('createdAt').ge(date)
或修改您的 table / 索引架构。
我很确定问题在于,当您在 createdAt
属性 上指定 rangeKey: true
时,您是在告诉它要用于全局索引(我不认为这是正确的术语)。该范围键将链接到 id
属性.
我认为最简单的解决方案是将您的 locationId
索引更改为如下所示:
index: {
global: true,
rangeKey: 'createdAt',
},
这样您就可以非常明确地说明要将 createdAt
设置为 rangeKey
的索引。
进行更改后,请记住将您的更改与本地 DynamoDB 服务器或实际的 DynamoDB 服务同步,以便在您的代码和数据库系统中填充更改。
希望这对您有所帮助!如果它不能解决您的问题,请随时发表评论,我会进一步帮助您。