格式:

db.collection.group({ key, reduce, initial, [keyf,] [cond,] finalize })

count、sum、avg统计查询


db.TestPaper.group({
 key : {"PaperType":true}, 
 cond : { "PaperType" : {"$in":[2,3,8]}}, 
 initial : {"count":0,"total":0}, 
 reduce : function Reduce(doc, out) {
	/*

	// At start of Reduce, 'out' = intial value
	out.count += doc.count; 
	
	*/
	out.count+=1;//数量统计count
	out.total+= doc.FaceQuestionCount;//求和统计sum
}, 
 finalize : function Finalize(out) {
	/*  
	
	// Make final updates or calculations
	out.countSquare = out.count * out.count
	
	*/
	out.avg=out.total/out.count;//平均数
	return out;
}
});


Logo

前往低代码交流专区

更多推荐