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