1

我有一个名为“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"?
4

1 回答 1

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,但您应该考虑将数据拆分到多个表中请参阅我在如何更新嵌入式列表中的项目中的评论?

于 2014-02-28T22:00:18.380 回答