How to update an item in a embedded list?

痞子三分冷 提交于 2019-12-11 09:26:43

问题


I have a josn named "update",and it has an embedded list "comments" like this:

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

   }]
 }

Questions are:

1, How to replace "old first level comment content..." with "new first level
   comment content..." by ids "update/0" and "comment/0"?

2, How to replace "old second level comment content..." with "new second level
   comment content..." by ids "update/0","comment/0" and "comment/00"?

回答1:


First the queries you are looking for:

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({
                    content: "new first level comment content..."
                }),
                comment
            )
        })
    })
}).run(...)

Second one:

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").map(function(child) {
                        return r.branch(
                            child("id").eq("comment/00"),
                            child.merge({
                                content: "new second level comment content..."
                            }),
                            child
                        )
                    })
                }),
                comment
            )
        })
    })
}).run(...)

You probably want to split your data in multiple tables to do joins. In your case, a table "update" and a table "comment". Your table comment can join itself for the children.

You can find more information here:

  • http://www.rethinkdb.com/docs/data-modeling/
  • http://www.rethinkdb.com/docs/table-joins/

If you have more questions, let me know.



来源:https://stackoverflow.com/questions/22062614/how-to-update-an-item-in-a-embedded-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!