0

我正在创建一个聊天应用程序并使用 Dynamoose.js。我有一张conversations桌子和一张messages桌子。我想在messages表上查询以查找属于对话的所有消息。当我进行查询时,我收到以下错误:

message: 'Invalid operator used in KeyConditionExpression: IN',
code: 'ValidationException',

这是我的模式和查询的样子:

对话.model.js

let schema = {
        conversation_id: {
            hashKey: true,
            type: String,
            required: true
        },

        participants: {
            type: Array,
            schema: [String],
            required: true
        },

        lastModified: {
            type: Date,
            required: true,
            default: Date.now
        }
    };

消息模型.js

let schema = {
        message_id: {
            hashKey: true,
            type: String,
            required: true,
            index: {
                name: 'conversationIndex',
                global: true,
                rangeKey: 'conversation_id',
                throughput: {'read': 10, 'write': 10}
            }
        },

        conversation_id: {
            rangeKey: true,
            type: String,
            required: true
        },

        from: {
            type: String,
            required: true
        },     

        message: {
            type: String,
            required: true
        },

        ts: {
            type: Date,
            required: true,
            default: Date.now
        }

询问

Message.query('conversation_id').in(userConversations).using('conversationIndex').exec((err, messages) => {
     // userConversations is an array of conversation_id's

     if (err) {
          console.error(err);
     } else {

     }
});

知道我要去哪里错了吗?我认为这可能与我创建和使用二级索引的方式有关,但我不太确定。任何帮助表示赞赏,谢谢!

4

1 回答 1

0

DynamoDB 查询要求您查询 where hashKey = ______。您不能在查询 hashKey 的地方使用in.

最重要的是,conversation_id您的索引中不是 hashKey。


有点难说,但看起来这是不符合 DynamoDB 模式的数据库设计的症状。我建议更多地研究 DynamoDB 表设计,并重新设计您的表以更好地适应 DynamoDB 模式。

于 2020-07-24T22:21:28.287 回答