在 dynamodb 中的排序键范围内查询

Kev*_*ith 1 c# amazon-web-services amazon-dynamodb

我正在尝试使用给定的分区键和排序键的范围查询 dynamodb 表,但 API 不断抛出以下错误:

Amazon.DynamoDBv2.AmazonDynamoDBException KeyConditionExpressions 每个键只能包含一个条件

这是用于创建请求的 C# 代码:

var partitionKey = 10;
var from = DateTime.NowUtc.AddDays(-1);
var to = DateTime.NowUtc;

var queryRequest = new QueryRequest
{
    TableName = _tableName,
    IndexName = "index",
    KeyConditionExpression = "#pkey = :v_pkey and #skey >= :v_from and #skey <= :v_to",
    ExpressionAttributeNames = {
        {"#pkey", "PartitionKey"},
        {"#skey", "SortKey"}
    },
    ExpressionAttributeValues = {
        {":v_pkey", new AttributeValue { N = partitionKey.ToString(CultureInfo.InvariantCulture) }},
        {":v_from", new AttributeValue { N = new DateTimeOffset(from).ToUnixTimeSeconds().ToString(CultureInfo.InvariantCulture) }},
        {":v_to", new AttributeValue { N = new DateTimeOffset(to).ToUnixTimeSeconds().ToString(CultureInfo.InvariantCulture) }}
    },
    ScanIndexForward = true
};

AmazonDynamoDBClient client = CreateClient();
var queryResponse = client.Query(queryRequest);
Run Code Online (Sandbox Code Playgroud)

F_S*_*O_K 7

像这样使用 BETWEEN KeyConditionExpression 运算符:

var partitionKey = 10;
var from = DateTime.NowUtc.AddDays(-1);
var to = DateTime.NowUtc;

var queryRequest = new QueryRequest
{
    TableName = _tableName,
    IndexName = "index",
    KeyConditionExpression = "#pkey = :v_pkey AND #skey BETWEEN :v_from AND :v_to",
    ExpressionAttributeNames = {
        {"#pkey", "PartitionKey"},
        {"#skey", "SortKey"}
    },
    ExpressionAttributeValues = {
        {":v_pkey", new AttributeValue { N = partitionKey.ToString(CultureInfo.InvariantCulture) }},
        {":v_from", new AttributeValue { N = new DateTimeOffset(from).ToUnixTimeSeconds().ToString(CultureInfo.InvariantCulture) }},
        {":v_to", new AttributeValue { N = new DateTimeOffset(to).ToUnixTimeSeconds().ToString(CultureInfo.InvariantCulture) }}
    },
    ScanIndexForward = true
};

AmazonDynamoDBClient client = CreateClient();
var queryResponse = client.Query(queryRequest);
Run Code Online (Sandbox Code Playgroud)