[英]Dynamo DB query not returning expected results when using nested collections
我有一個具有以下結構的 AWS DynamoDB 表:
我正在嘗試取回至少包含一個 ID 為 3401 的 RequestItem 的所有項目。這是我迄今為止嘗試過的(c# 代碼):
IAmazonDynamoDB client = new AmazonDynamoDBClient(
new BasicAWSCredentials(configuration["AccessKey"], configuration["SecretKey"]),
RegionEndpoint.USEast1);
var request = new ScanRequest
{
TableName = "dynamo-table-name",
ExpressionAttributeNames = new Dictionary<string, string>
{
{"#requestItems", "RequestItems"},
{"#requestId", "Id"}
},
ExpressionAttributeValues = new Dictionary<string, AttributeValue>
{
{":val", new AttributeValue {N = "3401"}}
},
FilterExpression = "contains(#requestItems.#requestId, :val)"
};
var response = await client.ScanAsync(request);
我對 FilterExpression 做了一些變化(使用簡單的“=”而不是“包含”)但是......我仍然沒有得到結果。 查詢通過而沒有錯誤,但結果是一個空列表。
但是,相同的代碼適用於不是集合的屬性(例如 Contact.EmailAddress)
我錯過了什么?
[編輯]
我嘗試了另一種建議的解決方案:
var request = new ScanRequest
{
TableName = "dynamo-table-name",
ExpressionAttributeNames = new Dictionary<string, string>
{
{"#requestItems", "RequestItems"}
},
ExpressionAttributeValues = new Dictionary<string, AttributeValue>
{
{
":val",
new AttributeValue
{
L = new List<AttributeValue>
{
{
new AttributeValue
{
M = new Dictionary<string, AttributeValue>
{{"Id", new AttributeValue {N = "3401"}}}
}
}
}
}
}
},
FilterExpression = "contains(#requestItems, :val)"
};
var response = await client.ScanAsync(request);
但我仍然沒有收到結果。
您無法真正使用 DynamoDB 執行您想要的查詢。 如果您知道RequestItems
中可能contains
的最大項目數量,那么您唯一可以做的就是使用OR
將大量contains
檢查鏈接在一起: (RequestItems.0.Id = :val) OR (RequestItems.1.Id = :val) OR (RequestItems.2.Id = :val) ...
. 不過,這似乎不是一個好主意,除非您事先知道RequestItems
將始終包含一定數量的項目。
contains
不能按您希望的方式工作。 如果您執行contains(path, <some number>)
,DynamoDB 會檢查在path
找到的值是否是一組數字以及<some number>
提供的值是否包含在該組中。
鑒於您的數據架構,恐怕您唯一的選擇是獲取所有項目並在您的代碼中過濾它們。
如果這不是權威,我深表歉意。
我懷疑 DynamoDB不能這樣做。
此外,DynamoDB 背后的想法是它不應該這樣做。
DynamoDB 不支持對數據進行任意函數評估。
DynamoDB 是一個 KV(有點)存儲,而不是一個數據庫。 “發電機”方式是查詢您可能需要的所有行(項目)並在客戶端分析列(鍵)。 請注意,它的成本完全相同(對於發電機,流量差異很小),因為 aws 會向您收取“數據庫磁盤讀取”之類的費用。 而且它既繁瑣又簡單,例如,您仍然必須處理分頁。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.