4

当有这样的嵌套文档时:

{
"blog": "testblog1",
"user_id": 41,
"comments": [
    {
        "comment": "testcomment1",
        "user_id": 883
    },
    {
        "comment": "testcomment2",
        "user_id": 790
    }
  ]
}

可以使用 N1QL 进行更新操作以向子文档添加额外字段吗?

我想将该字段添加"ranking" : "top comment"到带有注释 testcomment2 的子文档中:

{
"blog": "testblog1",
"user_id": 41,
"comments": [
    {
        "comment": "testcomment1",
        "user_id": 883
    },
    {
        "comment": "testcomment2",
        "user_id": 790,
        "ranking" : "top comment"
    }
  ]
}

站点注释:以下语句将在 Collection Blog 的根文档中添加一个字段:

UPDATE Blog SET rank = "top blog" WHERE blog = "testblog1";
4

1 回答 1

11

是的,您可以在 N1QL 中执行以下操作:

UPDATE Blog
SET c.ranking = "top comment" FOR c IN comments WHEN c.comment = "testcomment2" END
WHERE blog = "testblog1"

于 2015-04-14T04:57:16.320 回答