我有以下聚合代码,它返回我的学校 ID 列表中每所学校的所有学生。每个学生都有一个学年 ID,如下所示: ObjectId("5ede4682341e8426f1cf6285")
return await this.studentSchoolModel.aggregate([
{
$match: {
school: { $in: schoolIDs },
},
},
{
$group: {
_id: '$school',
academic_years: '$academic_year',
total_students: { $sum: 1 },
},
},
]);
如果我尝试按学校分组,它会起作用,但如果我尝试按学校和学年分组,则会出现以下错误:
MongoError: The field 'academic_year' must be an accumulator object
有谁知道我做错了什么?我希望每所学校的输出看起来像这样:
_id: 12345678 //schoolID
academic_years: {
_id: 1111111 //academic year ID
total_students: 5 //number of students in that year
},
{
_id: 2222222 //academic year ID
total_students: 8 //number of students in that year
}
答案 0 :(得分:0)
您可以使用def create_plot_scatter_bubble_wlabels(df,x_axis,y_axis,xlabel,ylabel,bubblesize,num_to_label):
sns.set_style("darkgrid", {"axes.facecolor": ".9"})
ax = sns.scatterplot(data=df , x=x_axis, y=y_axis,hue='categoryname' ,s=200,size=bubblesize,sizes=(200, 2000))
for i, txt in enumerate(df[:num_to_label].categoryname):
ax.annotate(txt, (df[:num_to_label][x_axis].iat[i],df[:num_to_label][y_axis].iat[i]))
plt.legend(bbox_to_anchor=(1.05, 2), loc=2, borderaxespad=0.)
plt.gcf().set_size_inches(12.7, 8.27)
plt.xlabel(xlabel, fontsize=16)
plt.ylabel(ylabel, fontsize=16)
这是代码
$group