我有一个收藏品,里面有很多文件。 例如其中一个文档。
"_id" : ObjectId("5c018f2c81d68232a4cd3bab"),
"id" : 1,
"first_name" : "Nata",
"last_name" : "Edy",
"email" : "nedy0@bravesites.com",
"gender" : "Female",
"phone" : "473-560-2667",
"group" : {
"group_id" : "gr02",
"form_of_educt" : "daytime",
"syllabus" : {
"titel_of_subj" : "INFO",
"subjects" : [
{
"sub_id" : 1,
"titel_of_sub" : "Mathematics",
"teacher" : "Ing. Hora",
"mark" : 3
},
{
"sub_id" : 2,
"titel_of_sub" : "Web Design",
"teacher" : "Ing. Masner",
"mark" : 1
},
{
"sub_id" : 3,
"titel_of_sub" : "Programming",
"teacher" : "Dr. Gates",
"mark" : 2
}
]
}
}
我需要从整个收藏集中找到数学主题的平均值 标记。 我想我需要使用Aggregation Framework,但我不了解如何将其用于对象数组。
Сan,请帮我。 预先谢谢你。
答案 0 :(得分:0)
此汇总作业将返回整个集合中的数学分数平均值
db.collection.aggregate([
{
$unwind: "$group.syllabus.subjects"
},
{
$match: {
"group.syllabus.subjects.titel_of_sub": "Mathematics"
}
},
{
$group: {
_id: null,
avg_math_marks: {
$avg: "$group.syllabus.subjects.mark"
}
}
}
])