2017-02-10 130 views
0

樣本文檔,我想一起工作是:更新嵌套數組n1ql

{ 
    "name": "level1", 
    "childLevel1": [ 
    { 
     "name": "level2", 
     "childLevel2": [ 
     { 
      "name": "level3", 
      "childLevel3": [ 
      { 
       "name": "level4", 
       "attribute1": "data" 
      } 
      ] 
     } 
     ] 
    } 
    ] 
} 

我們想要做的是,用n1ql增加一個數組元素,以「childLevel3」。我曾嘗試參考https://developer.couchbase.com/documentation/server/4.5/n1ql/n1ql-language-reference/update.html

我能夠將一個常規屬性添加到childLevel2,但在此之後它不起作用。這是我一直在嘗試的查詢。

Update sample 
set child2.childLevel3 = ARRAY_PUT(child2.childLevel3, {'attribute' : 'data2'}) 
FOR child2 in child1.childLevel2 when child2.name == "level3" END 
for child1 in childLevel1 when childLevel1.name == 'level2' END 
WHERE name == 'level1' 

但它給了我解析錯誤,我嘗試了其他方法,但沒有任何工作。

回答

1

嘗試

UPDATE sample 
SET child2.childLevel3 = ARRAY_PUT(child2.childLevel3, {'attribute' : 'data2'}) 
    FOR child2 in child1.childLevel2 
    FOR child1 in childLevel1 
    WHEN childLevel1.name == 'level2' AND child2.name == "level3" 
    END 
WHERE name == 'level1'; 
+0

它不添加任何東西,它不會給任何錯誤 – Ashish