在沙发上,任何聚合方法或模板(无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"
}]}
非常感谢!
答案 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);