File tree Expand file tree Collapse file tree 1 file changed +46
-2
lines changed Expand file tree Collapse file tree 1 file changed +46
-2
lines changed Original file line number Diff line number Diff line change @@ -65,9 +65,53 @@ mongoimport -d test -c grades --drop grades.json
65
65
### Response
66
66
67
67
````
68
-
68
+ db.grades.aggregate([
69
+ {
70
+ $unwind: '$scores'
71
+ },
72
+ {
73
+ $match: {
74
+ 'scores.type': { $in: ['exam', 'homework'] }
75
+ }
76
+ },
77
+ {
78
+ $group: {
79
+ _id: {
80
+ class_id: '$class_id',
81
+ student_id: '$student_id'
82
+ },
83
+ scoresAvg: { $avg: '$scores.score' }
84
+ }
85
+ },
86
+ {
87
+ $project: {
88
+ _id: 0,
89
+ student_id: '$_id.student_id',
90
+ class_id: '$_id.class_id',
91
+ scoresAvg: 1
92
+ }
93
+ },
94
+ {
95
+ $group: {
96
+ _id: {
97
+ class_id: '$class_id'
98
+ },
99
+ avg: {
100
+ $avg: '$scoresAvg'
101
+ }
102
+ }
103
+ },
104
+ {
105
+ $sort: {
106
+ 'avg': -1
107
+ }
108
+ },
109
+ {
110
+ $limit: 1
111
+ }
112
+ ])
69
113
````
70
114
71
- *
115
+ * 1
72
116
73
117
You can’t perform that action at this time.
0 commit comments