1

我有下面给出的格式的数据。

{
"_id" : 19,
"name" : "Gisela Levin",
"scores" : [
        {
                "score" : 44.51211101958831,
                "type" : "exam"
        },
        {
                "score" : 0.6578497966368002,
                "type" : "quiz"
        },
        {
                "score" : 93.36341655949683,
                "type" : "homework"
        }
],
"studentType" : "poor"}

现在我想找到每个studentType的平均考试成绩,我写了一个查询

db.student.aggregate( {$project : {_id : 0, studentType : 1, "scores.0.score" : 1}}, {$group : {_id : "$studentType", AverageMarks : {$avg : "$scores.0.score"}}} )

但我得到了这个答案

{ "_id" : "good", "AverageMarks" : null }

{“_id”:“avg”,“AverageMarks”:null } {“_id”:“差”,“AverageMarks”:null }

4

0 回答 0