汇总Couchbase文档数据

时间:2019-03-15 04:42:13

标签: couchbase

在沙发上,任何聚合方法或模板(无N1QL)用于聚合值??像这样的格式沙发文档

{"Students":[
    {
    "Student_id": "101",
    "Name": "Jon",
    "Course" : "Engineer"
    },
    {
    "Student_id": "102",
    "Name": "Snow",
    "Course" : "Medical"
    },
    {
    "Student_id": "103",
    "Name": "Walter",
    "Course" : "Chemistry"
    },
    {
    "Student_id": "104",
    "Name": "White",
    "Course" : "Chemistry"
    }]}

非常感谢!

1 个答案:

答案 0 :(得分:0)

您可以按以下方式使用N1QL。

INSERT INTO default VALUES("f001",{"Students":[ { "Student_id": "101", "Name": "Jon", "Course" : "Engineer" }, { "Student_id": "102", "Name": "Snow", "Course" : "Medical" }, { "Student_id": "103", "Name": "Walter", "Course" : "Chemistry" }, { "Student_id": "104", "Name": "White", "Course" : "Chemistry" } ] } );

SELECT (OBJECT v.Course||"Students":v.cnt
       FOR v IN ARRAY_APPEND(ds, {"Course":"", "cnt": ARRAY_SUM(ds[*].cnt)})
       END).*
LET ds = (SELECT s.Course|| " " AS Course , COUNT(1) AS cnt
           FROM default AS d
           UNNEST d.Students AS s
           GROUP BY s.Course);