如何从嵌入列表中删除项目?

时间:2014-02-27 07:59:06

标签: rethinkdb

我有一个名为" update"的josn,它有一个嵌入式列表" comments"像这样:

{
   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"?

1 个答案:

答案 0 :(得分: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?

中的评论