1
我在Cosmos中擁有Azure DocumentDB json存儲,並且我試圖在集合內應用一個互斥排除的條件where子句,該集合在集合內。我如何期待查詢工作沒有返回預期的結果!查詢Azure DocumentDB中的子集合的子集合
可以說我有一個產品類:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<ProductAttribute> ProductAttributes { get; set; }
}
具有ProductAttributes的集合,反過來,擁有字符串的集合:
public class ProductAttribute
{
public string Name { get; set; }
public IEnumerable<string> AttributeValues { get; set; }
}
如果我創建2個例品和將它們添加到DocumentDB集合中:
var product = new Product
{
Id = 1,
Name = "Banana",
ProductAttributes = new List<ProductAttribute>
{
new ProductAttribute{Name = "SourceCountry",AttributeValues = new List<string>{ "COL", "HON", "SA" }},
new ProductAttribute{Name = "Colours",AttributeValues = new List<string>{ "Yellow" }},
}
};
var product1 = new Product
{
Id = 2,
Name = "Grape",
ProductAttributes = new List<ProductAttribute>
{
new ProductAttribute{Name = "SourceCountry",AttributeValues = new List<string>{ "FRA", "ITA", "SA" }},
new ProductAttribute{Name = "Colours",AttributeValues = new List<string>{ "Red", "Green" }},
}
};
這些文檔存儲爲以下JSON:
{
"id": "1",
"name": "Banana",
"productAttributes": [
{
"name": "SourceCountry",
"attributeValues": [
"COL",
"HON",
"SA"
]
},
{
"name": "Colours",
"attributeValues": [
"Yellow"
]
}
]}
和
{
"id": "2",
"name": "Grape",
"productAttributes": [
{
"name": "SourceCountry",
"attributeValues": [
"FRA",
"ITA",
"SA"
]
},
{
"name": "Colours",
"attributeValues": [
"Red",
"Green"
]
}
]}
我想查詢產品只返回那些具有匹配跨越兩種類型ProductAttribute的標準,它的屬性。
的單一屬性下面的查詢確實返回這兩種產品預期(包括葡萄和香蕉包括「SA」的SourceCountry:
select p.name, p.productAttributes from c as p
join pa in p.productAttributes
join pav in pa.attributeValues
where (pa.name='SourceCountry' and pav = 'SA')
不過,我需要能夠跨應用標準這兩種類型的屬性,即「SourceCountry」和「顏色」的 - 所以我想下面的查詢:
select p.name, p.productAttributes from c as p
join pa in p.productAttributes
join pav in pa.attributeValues
where (pa.name='SourceCountry' and pav = 'SA')
and (pa.name='Colours' and pav = 'Red')
我期待「葡萄」從上面的查詢返回,因爲這產品是同時擁有唯一的一個'SourceCou 'SA'的'ntry'和'紅色'的'顏色'屬性。
但是,沒有產品返回,我非常感謝有人可以解釋爲什麼是這種情況!
非常感謝 - 這確實工作。當我在我的實際用例中有10多個屬性類型時,這種方法變得有點麻煩!我還發現我收到了重複的文檔,但我認爲未來會有'獨特'功能:https://feedback.azure.com/forums/263030-documentdb/suggestions/6719531-provide-support-for-distinct – abrown
這個答案確實解決了當前的問題,但不幸的是,在兩種以上的屬性間匹配是不可持續的 - DocumentDB每個查詢限制5個連接!因此,不支持pa3和pav3添加另一個限制。 – abrown