{ "_id" : 1, "quizzes" : [ 10, 6, 7 ], "labs" : [ 5, 8 ], "final" : 80, "midterm" : 75 ,"extraMarks":10}
{ "_id" : 2, "quizzes" : [ 9, 10 ], "labs" : [ 8, 8 ], "final" : 95, "midterm" : 80 }
{ "_id" : 3, "quizzes" : [ 4, 5, 5 ], "labs" : [ 6, 5 ], "final" : 78, "midterm" : 70 }
这些是我收藏的文件。 使用
中建议的管道查询$add with some fields as Null returning sum value as Null
我可以使用此查询投影字段总和:
db.students.aggregate([ { "$project": { "final": 1, "midterm": 1, "examTotal": { "$add": [ "$final", "$midterm", { "$ifNull": [ "$extraMarks", 0 ] } ] } } }]);
现在我们必须在学生集合中更新一个名为total as field的新字段,类似于上述预测中的考试总数?