如何查询嵌入数据documentDB以检索父文档

时间:2017-07-26 16:28:34

标签: linq azure-cosmosdb

在DocumentDb中,是否可以搜索子文档以获取父文档?

public class Customer
{
    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; } 

    [JsonProperty(PropertyName = "locations")]
    public List<Location> Locations { get; set; }

    public Customer()
    {
        Locations = new List<Location>();
    }
}

public class Location
{
    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "city")]
    public string City{ get; set; }

    [JsonProperty(PropertyName = "state")]
    public string State{ get; set; } 

}

在Document Explorer中,我可以看到我有这样一个类结构的实例:

{
  "id": "7",
  "name": "ACME Corp", 
  "location": [
    {
      "id": "c4202793-da55-4324-88c9-b9c9fe8f4b6c",
      "city": "newcity",
      "state": "ca" 
    }
  ]
},
{
  "id": "35",
  "name": "Another Corp", 
  "location": [
    {
      "id": "d33e793-da55-4324-88c9-b9c9fe8f4baa",
      "city": "newcity",
      "state": "ca" 
    }
  ]
}

有没有办法查询嵌入数据,例如city =&#39; newcity&#39;和州=&#39; ca&#39; 但检索父数据?如果我使用SelectMany(x =&gt; x.Locations)来查询子项,那么它将获取位置数据而不是根(客户)文档。

感谢

1 个答案:

答案 0 :(得分:2)

  

有没有办法查询嵌入数据,例如city =&#39; newcity&#39;和州=&#39; ca&#39;但检索父数据?

是的,我们可以使用加入来执行此操作。更多详情请参阅Advanced database concepts and SQL queries

df.groupby("id")["indicator"].fillna(value=None,method="ffill",limit=3) 

返回

enter image description here

C#代码演示:

SELECT c.id as id ,c.name as name,l as location from customer c Join l in 
c.location where l.city = 'newcity' and l.state = 'ca'

enter image description here