如何查询嵌入式数据documentDB以检索父文档 [英] How to query embedded data documentDB to retrieve parent document

查看:92
本文介绍了如何查询嵌入式数据documentDB以检索父文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In DocumentDb, is it possible to search for child documents to get the parent documents?

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; } 

}

在文档资源管理器中,我可以看到我有一个此类结构的实例,如下所示:

In the Document Explorer, I can see that I have one instance of this class structure like so:

{
  "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 ='newcity'和state ='ca' 但是检索父数据?如果我使用SelectMany(x => x.Locations)查询子项,则它将获取位置数据,而不是根(客户)文档.

Is there a way to query the embedded data like where city = 'newcity' and state = 'ca' but retrieve parent data? if I use SelectMany(x => x.Locations) to query for child then it will get Location data not the root(Customer) document.

谢谢

推荐答案

是否有一种查询嵌入数据的方法,例如city ='newcity'和state ='ca'但可以检索父数据?

Is there a way to query the embedded data like where city = 'newcity' and state = 'ca' but retrieve parent data?

是的,我们可以使用 Join 来做到这一点.有关更多详细信息,请参阅高级数据库概念和SQL查询.

Yes, we may use Join to do that. More details please refer to Advanced database concepts and SQL queries.

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'

返回哪个

C#代码演示:

FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1 };
var customerQuery = client.CreateDocumentQuery<dynamic>(
UriFactory.CreateDocumentCollectionUri(databaseName, collectionName),
                "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'",
                queryOptions).AsDocumentQuery();

var customerList = new List<dynamic>();
while (customerQuery.HasMoreResults)
{

   customerList.AddRange(customerQuery.ExecuteNextAsync<dynamic>().Result);

}

这篇关于如何查询嵌入式数据documentDB以检索父文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆