[英]Azure Cosmos DB - difference in how the partition key is supplied to query?
[英]Why Linq query doesn't return records when querying against partition key in Cosmos Db?
我正在嘗試檢索與 Cosmos DB 中的分區鍵匹配的記錄。 我正在使用以下代碼:
//sec.id is Guid, s.key is the partition key (also a Guid)
var query = sectionsContainer.GetItemLinqQueryable<Post>().Where(s => s.type == "post" && s.key == sec.id);
var qdef = query.ToQueryDefinition();
var iterator = postsContainer.GetItemQueryIterator<Post>(qdef);
List <Post> posts = new List<Post>();
while (iterator.HasMoreResults)
{
var response = await iterator.ReadNextAsync();
posts.AddRange(response.ToList());
}
return posts;
Cosmos DB 不返回任何內容。 SQL 查詢 Linq 生成如下:
SELECT VALUE root FROM root WHERE ((root["type"] = "post") AND (root["key"] = "9bb4fcdc-2077-4a0b-9041-81752f06d011"))
反過來,當復制並粘貼到 Azure 門戶的數據資源管理器時,查詢返回正確的結果:
[
{
"id": "52d2abbd-d777-4c3c-b02c-a940eb49fdc4",
"type": "post",
"key": "9bb4fcdc-2077-4a0b-9041-81752f06d011", //Doesn't work when querying against this
...
"section": {
"id": "9bb4fcdc-2077-4a0b-9041-81752f06d011", //Works when querying against this
"type": "section",
...
}
//Data removed for brevity
},
// Second & third item...
]
當我修改查詢以匹配 [document].section.id == sec.id,Linq / Cosmos DB 返回正確的數據。
這里的問題是為什么查詢在使用分區鍵時不起作用但在使用任何其他屬性時起作用?
問題出在鍵盤和椅子之間。
查詢是針對sections -container 生成的,但針對posts 容器運行。
var query = sectionsContainer.GetItemLinqQueryable<Post>().Where(s => s.type == "post" && s.key == sec.id);
var qdef = query.ToQueryDefinition();
//var iterator = postsContainer.GetItemQueryIterator<Post>(qdef); <-- Problem was here.
var iterator = sectionsContainer.GetItemQueryIterator<Post>(qdef); // Now it works
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.