仅在主(分区)键上使用 Between 运算符的 DynamoDb 查询
DynamoDb Query using Between operator on Primary(Partition) Key only
我有一个 Dynamo table,其中只有分区键 - “CreatedAt”和一些其他属性。
我想写一个我不会得到的查询。 CreatedAt 介于 LastSunday 和 Today 之间的项目数。
我已将日期以字符串格式存储为“2021-03-09T14:33:29Z”。
我不想使用 GSI。
使用以下查询时出现错误 - “不支持查询关键条件”
这是我的代码:
var queryRequest = new QueryRequest
{
TableName = myTableName,
KeyConditionExpression = "CreatedAt BETWEEN :LastMonday AND :TodaysDate",
};
Dictionary<string, AttributeValue> expressionAttributeValues = new Dictionary<string, AttributeValue>();
expressionAttributeValues.Add(":TodaysDate", new AttributeValue { S = "2021-03-09T14:33:29Z" });
expressionAttributeValues.Add(":LastMonday", new AttributeValue { S = "2021-03-02T14:33:29Z" });
queryRequest.ExpressionAttributeValues = expressionAttributeValues;
var response = await awsDynamoClient.QueryAsync(queryRequest);
DynamoDB 不支持对分区键的条件操作。
来自docs:
Use the KeyConditionExpression parameter to provide a specific value for the partition key. The Query operation will return all of the items from the table or index with that partition key value. You can optionally narrow the scope of the Query operation by specifying a sort key value and a comparison operator in KeyConditionExpression.
query operation requires that you specify the full partition key. The getItem 操作要求您提供完整 主键。没有通过指定分区键的特征(例如 begins_with、between 等)进行搜索的选项。另一方面,排序键是专门为此目的构建的。
如果你想进行范围查询,当你的CreatedAt
属性是排序键时,你应该定义一个composite primary key。复合主键具有分区键 和排序键 。排序键将允许您使用您描述的 between
条件获取数据。
编辑:为了完整起见,我要补充一点,您 可以 执行 scan
操作,您可以在其中对分区键应用条件。很少有(如果有的话)这是个好主意的例子。这是一个应该避免的昂贵操作,除非你真的知道你在做什么。
我有一个 Dynamo table,其中只有分区键 - “CreatedAt”和一些其他属性。
我想写一个我不会得到的查询。 CreatedAt 介于 LastSunday 和 Today 之间的项目数。
我已将日期以字符串格式存储为“2021-03-09T14:33:29Z”。
我不想使用 GSI。
使用以下查询时出现错误 - “不支持查询关键条件”
这是我的代码:
var queryRequest = new QueryRequest
{
TableName = myTableName,
KeyConditionExpression = "CreatedAt BETWEEN :LastMonday AND :TodaysDate",
};
Dictionary<string, AttributeValue> expressionAttributeValues = new Dictionary<string, AttributeValue>();
expressionAttributeValues.Add(":TodaysDate", new AttributeValue { S = "2021-03-09T14:33:29Z" });
expressionAttributeValues.Add(":LastMonday", new AttributeValue { S = "2021-03-02T14:33:29Z" });
queryRequest.ExpressionAttributeValues = expressionAttributeValues;
var response = await awsDynamoClient.QueryAsync(queryRequest);
DynamoDB 不支持对分区键的条件操作。
来自docs:
Use the KeyConditionExpression parameter to provide a specific value for the partition key. The Query operation will return all of the items from the table or index with that partition key value. You can optionally narrow the scope of the Query operation by specifying a sort key value and a comparison operator in KeyConditionExpression.
query operation requires that you specify the full partition key. The getItem 操作要求您提供完整 主键。没有通过指定分区键的特征(例如 begins_with、between 等)进行搜索的选项。另一方面,排序键是专门为此目的构建的。
如果你想进行范围查询,当你的CreatedAt
属性是排序键时,你应该定义一个composite primary key。复合主键具有分区键 和排序键 。排序键将允许您使用您描述的 between
条件获取数据。
编辑:为了完整起见,我要补充一点,您 可以 执行 scan
操作,您可以在其中对分区键应用条件。很少有(如果有的话)这是个好主意的例子。这是一个应该避免的昂贵操作,除非你真的知道你在做什么。