DynamoDB:如何通过ID到达嵌套对象?

时间:2019-01-14 18:58:05

标签: c# .net amazon-web-services amazon-dynamodb aws-sdk

我的DynamoDB中有两个表:

[DynamoDBTable("TVShow")]
public class TVShow
{
    [DynamoDBHashKey]
    public string Id { get; set; }

    [DynamoDBRangeKey]
    public long Timestamp { get; set; }

    [DynamoDBProperty]
    public string Title { get; set; }

    [DynamoDBProperty]
    public double Rate { get; set; }

    [DynamoDBProperty("Episodes")]
    public List<Episode> Episodes { get; set; }
}

[DynamoDBTable("Episode")]
public class Episode
{
    [DynamoDBHashKey]
    public string Id { get; set; }

    [DynamoDBRangeKey]
    public long Timestamp { get; set; }

    [DynamoDBProperty]
    public string Title { get; set; }
}

样本数据:

{
  "Episodes": {
    "L": [
      {
        "M": {
          "Id": {
            "S": "8ab47e73-188c-43e5-8010-1e22a982bb28"
          },
          "Timestamp": {
            "N": "636830978394004950"
          },
          "Title": {
            "S": "First Episode"
          }
        }
      },
      {
        "M": {
          "Id": {
            "S": "c477e288-37d1-4d7f-9104-9b72ed304808"
          },
          "Timestamp": {
            "N": "636830978394669182"
          },
          "Title": {
            "S": "Second Episode"
          }
        }
      }
    ]
  },
  "Id": {
    "S": "466d8a18-f998-49b3-a299-90b508dba045"
  },
  "Rate": {
    "N": "9.3"
  },
  "Timestamp": {
    "N": "636830978390056778"
  },
  "Title": {
    "S": "Vikings"
  }
}
  

Id:HASH |分区键

     

时间戳记:RANGE |排序键

我想通过[Episode] ID获得TVShow的“ Episode First”嵌套对象。

我尝试了多种方法来获取数据。但是没有用。这些是我的资料。这些文件不是最新的。我正在使用AWSSDK.DynamoDBv2 nuget软件包v3.3.16.5。

查询表和索引:.NET https://docs.aws.amazon.com/en_us/amazondynamodb/latest/developerguide/LowLevelDotNetQuerying.html

扫描表和索引:.NET https://docs.aws.amazon.com/en_us/amazondynamodb/latest/developerguide/LowLevelDotNetScanning.html

// I can get the TVShow by Id with this.
var scanRequest = new ScanRequest(nameof(TVShow))
{
    FilterExpression = "Id = :Id",
    ExpressionAttributeValues = new Dictionary<string, AttributeValue>
    {
        {":Id", new AttributeValue {S = "466d8a18-f998-49b3-a299-90b508dba045"}}
    }
};

// In the document scan is not async but in v3.3.16.5 scan is async.
var scanResponse = await Client.ScanAsync(scanRequest);


// I can get the TVShow by Id with this.
var queryRequest = new QueryRequest(nameof(TVShow))
{
    KeyConditionExpression = "Id = :Id",
    ExpressionAttributeValues = new Dictionary<string, AttributeValue>
    {
        {":Id", new AttributeValue {S = "466d8a18-f998-49b3-a299-90b508dba045"}}
    }
};

// In the document query is not async but in v3.3.16.5 query is async.
var queryResponse = await Client.QueryAsync(queryRequest);

这没关系。但是正如我所说,我想按嵌套对象ID或其他字段(如“标题”)进行过滤。最好的方法是什么?如果可以作为Table类。

0 个答案:

没有答案