如何更新嵌入列表中的项目? [英] How to update an item in a embedded list?

查看:45
本文介绍了如何更新嵌入列表中的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为update"的 josn,它有一个嵌入的列表评论",如下所示:

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[...]
                  }
                 ]

   }]
 }

问题是:

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

推荐答案

首先是您要查找的查询:

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(...)

第二个:

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:

如果您有更多问题,请告诉我.

If you have more questions, let me know.

这篇关于如何更新嵌入列表中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆