2014-02-27 21 views
1

我有一個josn名爲 「更新」,它有一個嵌入列表 「意見」 是這樣的:如何從嵌入列表中刪除項目?

{ 
    id: "update/0", 
    //comments contains elements with type:comment 
    comments: [{ 
     id:"comment/0" 
     children:[{ 
        id:"comment/00", 
         children[...] 
        }] 
    }] 
} 

的問題是:

1, How to remove an item from update's field:comments by ids "update/0" and 
    "comment/0"? 

2, How to remove an item from comment's field:children by ids "update/0"," 
    comment/0" and "comment/00"? 

回答

0
r.table("update").get("update/0").update(function(doc) { 
    return doc.merge({ 
     comments: doc("comments").concatMap(function(comment) { 
      return r.branch(
        comment("id").eq("comment/0"), 
        [], 
        [comment] 
      ) 
     }) 
    }) 
}) 

r.table("update").get("update/0").update(function(doc) { 
    return doc.merge({ 
     comments: doc("comments").map(function(comment) { 
      return r.branch(
        comment("id").eq("comment/0"), 
        comment.merge({ 
         children: comment("children").concatMap(function(child) { 
          return r.branch(
           child("id").eq("comment/00"), 
           [], 
           [child] 
         ) 
         }) 
        }), 
        comment 
      ) 
     }) 
    }) 
}) 

你也可以做到這一點與deleteAt ,但你應該考慮分割你的數據在多個表中 請參閱我的評論How to update an item in a embedded list?