2016-06-28 96 views
1

我想從包含雙重嵌套數組的大型文檔投影到數組的扁平表示中,而且我堅持如何繼續。DocumentDB子查詢

我有類似這樣的文件:

{ 
    "id": "1", 
    "themeId": "e4d3549c-2785-4067-83d6-f396d2212776", 
    "enabled": false, 
    "archived": false, 
    "componentGroups": [ 
     [ 
     { 
      "componentType": "header", 
      "enabled": true, 
      "configurationVariables": { 
      "text1": "AAA", 
      "text2": "BBB" 
      } 
     } 
     ], 
     [ 
     { 
      "componentType": "prompt", 
      "enabled": true, 
      "configurationVariables": { 
      "text1": "AAA", 
      "text2": "BBB" 
      } 
     }, 
     { 
      "componentType": "proactive", 
      "enabled": true, 
      "configurationVariables": { 
      "text1": "AAA", 
      "text2": "BBB" 
      } 
     } 
     ], 
     [ 
     { 
      "componentType": "product-feed", 
      "enabled": true, 
      "configurationVariables": { 
      "text1": "AAA", 
      "text2": "BBB" 
      } 
     } 
     ] 
    ] 
    } 

我試圖把它投射到以下結構:

{ 
    "id": "275973", 
    "themeId": "e4d3549c-2785-4067-83d6-f396d2212776", 
    "enabled": false, 
    "archived": false, 
    "components": [ 
     { 
      "componentType": "header", 
      "enabled": true 
     }, 
     { 
      "componentType": "prompt", 
      "enabled": true, 
     }, 
     { 
      "componentType": "proactive", 
      "enabled": true, 
     }, 
     { 
      "componentType": "product-feed", 
      "enabled": true 
     } 
     ] 
    ] 
    } 

我使用下面的查詢取得了一些成功

SELECT T.id, 
    T.themeId, 
    T.enabled, 
    T.archived, 
    [ { type: C.componentType, enabled: C.enabled } ] AS components 
FROM Panels T 
JOIN CG IN T.componentGroups 
JOIN C IN CG 
WHERE T.id IN ("275973") 

但是,這會爲每個組件類型返回一個單獨的記錄。我試圖將它們全部摺疊在一起,以便所有組件都位於包含文檔的單個實例中。我希望能夠像做一個嵌套的SELECT,我可以與外部文件加入,與此類似:

SELECT T.id, 
T.themeId, 
T.enabled, 
T.archived, 
[ 
    SELECT C.componentType, C.enabled 
    FROM CG IN T.componentGroups 
    JOIN C IN CG 
] AS components 
FROM Panels T 
WHERE T.id IN ("275973") 

但這是無效的。我正在尋找有關子/嵌套選擇的信息,並通過鑽入嵌套數組來返回數據。

+0

爲了確保我明白這個問題,你要刪除從子元素的'configurationVariables'場和扁平的組件列表中,右鍵?此外,預期輸出中還有許多其他字段未出現在文檔示例中。那些已經在頂層?我打算提供的答案是一個UDF,它可以爲你做「投影」。我懷疑你的建議可以用純SQL完成,但我發現JavaScript UDF比使用SQL更容易完成這些轉換。 –

+0

嗨拉里。更正了預計的文件。是的,你想要達到的目標是正確的。我現在想避免UDF,因爲我沒有一個好的策略來管理它們,因爲我期望它在應用程序啓動時同步,但應用程序已部署到大量機器上,所以我可以看到這造成問題。 –

+0

我的系統中有一個主節點,每次引導時都會重新加載所有Sprocs和UDF。我也可以隨時觸發某些事情來做到這一點。但是如果你真的想避免UDF,那麼我不知道如何去做你所要求的。其他人可能會。我看到一些SQL高手回答了這些問題。 –

回答

0

計劃對子查詢提供DocumentDB支持,但目前不支持。同時,UDF或將數據客戶端拉爲N條記錄,然後重新格式化是今天最好的方式。對於有興趣的人,這裏是在查詢返回的結果的UDF,

function transform(doc) { 
    var result = {}; 

    for (var prop in doc) { 
     if (prop != "componentGroups") { 
      result[prop] = doc[prop]; 
     } else { 
      result["components"] = []; 
      for(var cgidx in doc["componentGroups"]) { 
       var componentGroup = doc["componentGroups"][cgidx]; 
       for (var cidx in componentGroup) { 
        var component = componentGroup[cidx]; 
        result["components"].push({componentType: component.componentType, enabled: component.enabled }); 
       } 
      } 
     } 
    } 

    return result; 
}