Dynamoose 和 DynamoDB 二级索引搜索

问题描述

我正在尝试根据 2 个属性搜索表。但是我收到以下错误

ValidationException: 查询条件缺少关键架构元素

我在做什么:

let value = 'dsadsada';
Cache.query({'registration': { 'eq': value},'derivativeId': { 'eq': null}}).exec();

预期结果: 返回注册与值匹配但derivativeId为空的文档列表

我使用的是 dynamoose,所以首先这是模型:

import {model} from "dynamoose";
import {Document} from 'dynamoose/dist/Document';
import {Schema} from 'dynamoose/dist/Schema';

class Cache extends Document {
    id: string;
    createdAt: Date;
    updatedAt: Date;
}

const schema = {
    types: {
        id: {
            type: String,required: true
        },registration: {
            type: String,index: {
                name: 'VehicleLookupIndex',global: true
            }
        },derivativeId: {
            type: String,index: {
                name: 'VehicleLookupIndex'
            }
        }
    },options: {
        timestamps: true,saveUnkNown: ['**'],expires: 10
    },};

model.defaults.set({
    create: false,prefix: process.env.TABLE_PREFIX
});

export default model<Cache>('cache',new Schema(schema.types,schema.options));

这是我定义表格的 serverless 文件

cache:
  Type: AWS::DynamoDB::Table
  Properties:
    TableName: ${self:provider.stage}-${self:service}-cache
    AttributeDeFinitions:
      - AttributeName: id
        AttributeType: S
    KeySchema:
      - AttributeName: id
        KeyType: HASH
    BillingMode: PAY_PER_REQUEST
    GlobalSecondaryIndexes:
      - IndexName: 'VehicleLookupIndex'
        KeySchema:
          - AttributeName: 'id'
            KeyType: 'HASH'
        Projection:
          NonKeyAttributes:
            - 'derivativeId'
            - 'registration'
          ProjectionType: 'INCLUDE'

知道我做错了什么吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)