2016-05-16 38 views
2

是否可以使用N1QL?N1QL:向已有的couchbase文檔添加新對象

例如,我有這樣的:

{ 
"blog": "Coffee", 
"user_id": 41, 
"comments": [ 
    { 
     "comment": "cup", 
     "user_id": 883 
    }, 
    { 
     "comment": "water", 
     "user_id": 790 
    } 
    ] 
} 

,我想用N1QL加白糖評論,導致此: { 「博客」: 「咖啡」, 「USER_ID」:41, 「意見」:[{ 「評論」: 「杯」, 「USER_ID」:883 },{ 「評論」: 「水」, 「USER_ID」:790 },{ 「評論」: 「糖」, 「USER_ID」:14 } ] }

我嘗試這樣做:

UPDATE 
    Blog 
SET 
    `c.comment` = "sugar", 
    `c.user_id` = 14 
FOR 
    c IN comments 
WHERE 
    `blog` = "Coffee" 
// [{"code":3000,"msg":"syntax error - at WHERE"} 

而且這樣的:

UPDATE 
    Blog 
SET 
    ("comments", { "comment": "sugar", "user_id": 14}) 
WHERE 
    `blog` = "Coffee" 
//[{"code":3000,"msg":"syntax error - at ("} 

回答

3

是的,你可以使用N1QL執行任何修改。

UPDATE Blog 
SET comments = ARRAY_APPEND(comments, { "comment":"sugar", "user_id":14 }) 
WHERE blog = "Coffee"; 
+0

它的工作原理!謝謝! – Dimitar