json - N1QL : add new object to existed couchbase document

标签 json couchbase sql++ database nosql

可以使用N1QL吗?

例如我有这个:

{
"blog": "Coffee",
"user_id": 41,
"comments": [
    {
        "comment": "cup",
        "user_id": 883
    },
    {
        "comment": "water",
        "user_id": 790
    }
  ]
}

我想使用 N1QL 在评论中添加糖分,结果如下: { “博客”:“咖啡”, “用户id”:41, “评论”: [ { "评论": "杯子", “用户 ID”:883 }, { “评论”:“水”, “用户 ID”:790 }, { “评论”:“糖”, “用户 ID”:14 } ] }

我尝试过这个:

UPDATE
    Blog
SET
    `c.comment` = "sugar",
    `c.user_id` = 14
FOR
    c IN comments
WHERE 
    `blog` = "Coffee" 
// [{"code":3000,"msg":"syntax error - at WHERE"}

还有这个:

UPDATE 
    Blog
SET
    ("comments", { "comment": "sugar", "user_id": 14})
WHERE
    `blog` = "Coffee"
//[{"code":3000,"msg":"syntax error - at ("}

最佳答案

是的,您可以使用 N1QL 执行任何修改。

UPDATE Blog
SET comments = ARRAY_APPEND( comments, { "comment":"sugar", "user_id":14 } )
WHERE blog = "Coffee";

关于json - N1QL : add new object to existed couchbase document,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37258080/

相关文章:

javascript - 如何获取数组中所有json键

JQUERY JSON AJAX 可以离线工作,但不能在线工作

nosql - 为什么 Couchbase 对 json 文档中的数值进行舍入?

javascript - Couchbase、减少过大误差

java - 如何在自定义 Spring Boot N1QL 查询中使用百分号和 LIKE 来与 Couchbase DB 一起使用

android - 当字符串不是嵌入式资源时,如何设置 TextView 以显示 UTF-8?

java - 如何在 Java 中将新的类对象添加到现有的 JSON 文件中?

java.lang.UnsupportedOperationException : No couch port for cache buckets

python - 当我在 couchbase 集合之间移动文档时,如何添加移动时间?

couchbase - Couchbase 中的计数查询速度太慢