【问题标题】:DynamoDB/Dynamoose error using query with .in() operator使用带有 .in() 运算符的查询时出现 DynamoDB/Dynamoose 错误
【发布时间】:2020-07-25 06:21:28
【问题描述】:

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

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

这是我的架构和查询的样子:

conversation.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
        }
    };

message.model.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 {

     }
});

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

【问题讨论】:

    标签: node.js amazon-dynamodb dynamodb-queries dynamoose


    【解决方案1】:

    DynamoDB 查询要求您查询hashKey = ______ 的位置。您无法在使用 in 查询 hashKey 的情况下进行查询。

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


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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-26
      • 2018-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-14
      • 2017-10-25
      • 1970-01-01
      相关资源
      最近更新 更多