2017-07-26 9 views
0

DocumentDbでは、親文書を取得するために子文書を検索できますか?どこの都市=「newcity」のような埋め込まれたデータを照会する方法はあり埋め込みデータdocumentDBを照会して親文書を取得する方法

{ 
    "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" 
    } 
    ] 
} 

ドキュメントエクスプローラで
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; } 

} 

、私はそうのように、このクラス構造体のインスタンスを1つ持っていることがわかりますstate = 'ca' 親データを取得しますか? SelectMany(x => x.Locations)を使用して子を照会すると、ルート(顧客)文書ではなくロケーションデータが取得されます。

おかげ

答えて

2

どこの都市=「newcity」と状態は=「CA」のような埋め込まれたデータを照会するが、親のデータを取得する方法はありますか?

はい、を使用してください。に参加してください。詳細はAdvanced database concepts and SQL queriesを参照してください。コードも画面キャプチャのための多くの

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

} 

enter image description here

+0

うわー、おかげ:

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' 

! – wil

関連する問題