ใช้ $avg ใน Aggregate Framework
ตัวดำเนินการ $avg ร่วมกับ aggregate ใช้คำนวณค่าเฉลี่ยของฟิลด์ตัวเลข เริ่มต้นด้วยการสร้างคอลเลกชันพร้อมคะแนนนักเรียน
> db.averageReturiningNullDemo.insertOne({ "StudentDetails": { "StudentScore": 89 } });
คำสั่งคำนวณค่าเฉลี่ย:
> db.averageReturiningNullDemo.aggregate([ { "$group": { "_id": null, "StudentScoreAverage": { "$avg": "$StudentDetails.StudentScore" } } } ]);
ผลลัพธ์: { "_id" : null, "StudentScoreAverage" : 67 }