首页 > 解决方案 > AWS Appsync - 查询 DynamoDB 的空索引

问题描述

我想获取一个附加到QueryAppSync 模式的 DynamoDB 表,以便仅获取具有全局二级索引 (GSI) 的属性的 空值或空值。

我尝试了以下但无法得到结果:

抛出 [KeyConditionExpression: OR] 中使用的无效运算符:

"operation" : "Query",
"index" : "myAttrIndex",
"query" : {
    "expression" : "attribute_not_exists(myAttr) or myAttr = :null",
    "expressionValues" : {
        ":null" : { "NULL" : null }
    }
}

抛出 [KeyConditionExpression 中使用的无效运算符:attribute_not_exists]:

"operation" : "Query",
"index" : "myAttrIndex",
"query" : {
    "expression" : "myAttr = :null",
    "expressionValues" : {
        ":null" : { "NULL" : null }
    }
}

抛出 [一个或多个参数值无效:条件参数类型与架构类型不匹配]:

"operation" : "Query",
"index" : "myAttrIndex",
"query" : {
    "expression" : "myAttr = :null",
    "expressionValues" : {
        ":null" : { "NULL" : null }
    }
}

如何编写过滤掉字符串属性的非空值的查询文档?

标签: amazon-dynamodbaws-appsync

解决方案


默认情况下,全局二级索引是稀疏的。

对于表中的任何项目,如果项目中存在索引键值,DynamoDB 只会将相应的条目写入全局二级索引。对于全局二级索引,这是索引分区键及其排序键(如果存在)。如果索引键值没有出现在每个表项中,则称该索引是稀疏的。

如果您需要myAttr将属性空值包含在 GSI 表中,您可以设置一个虚拟值(例如“NULL”),然后查询该值。请注意,所有表格项目现在都将包含在 GSI 表格中,这将增加您的成本。

有关更多详细信息: https ://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-indexes-general-sparse-indexes.html


推荐阅读