/ *我是MongoDB的新手,并试图弄清楚如何获得课程CSIS2300的平均correct_answers
。
There is a part of the document. Some users take more than one course */
{
"_id" : 1.0,
"user_id" : "jjackson0",
"first_name" : "Jack",
"last_name" : "Jackson",
"email" : "jjackson0@apache.org",
"status" : "active",
"join_date" : "2014-12-10",
"last_login_date" : "2016-09-30 23:51:41 -0400",
"strengths" : [
"mongo queries",
"mongo map-reduce queries"
],
"courses" : [
{
"code" : "CSIS2300",
"total_questions" : 165.0,
"correct_answers" : 153.0,
"incorect_answers" : 12.0
}
]
}
/* 2 */
{
"_id" : 2.0,
"user_id" : "ecoleman1",
"first_name" : "Eugene",
"last_name" : "Coleman",
"email" : "ecoleman1@techcrunch.com",
"age" : 49.0,
"status" : "banned",
"join_date" : "2015-07-01",
"last_login_date" : "2016-09-30 23:54:08 -0400",
"address" : {
"city" : "Chencun",
"province" : "PEI"
},
"strengths" : [
"visualization",
"sql",
"query optimisation",
"dimensional modelling",
"analytics research"
],
"courses" : [
{
"code" : "CSIS2300",
"total_questions" : 188.0,
"correct_answers" : 106.0,
"incorect_answers" : 82.0
},
{
"code" : "CSIS3300",
"total_questions" : 12.0,
"correct_answers" : 9.0,
"incorect_answers" : 3.0
},
{
"code" : "CSIS3380",
"total_questions" : 172.0,
"correct_answers" : 142.0,
"incorect_answers" : 30.0
},
{
"code" : "CSIS3360",
"total_questions" : 140.0,
"correct_answers" : 21.0,
"incorect_answers" : 119.0
},
{
"code" : "CSIS4260",
"total_questions" : 163.0,
"correct_answers" : 75.0,
"incorect_answers" : 88.0
}
]
}
/* 3 */
{
"_id" : 3.0,
"user_id" : "mbowman2",
"first_name" : "Mark",
"last_name" : "Bowman",
"email" : "mbowman2@ebay.co.uk",
"age" : 36.0,
"status" : "disabled",
"join_date" : "2015-02-01",
"last_login_date" : "2016-09-30 23:58:07 -0400",
"address" : {
"city" : "Ban Kruat",
"province" : "NL"
},
"strengths" : [
"mongo map-reduce queries",
"sql",
"dimensional modelling",
"visualization"
],
"courses" : [
{
"code" : "CSIS2300",
"total_questions" : 185.0,
"correct_answers" : 171.0,
"incorect_answers" : 14.0
},
{
"code" : "CSIS3300",
"total_questions" : 57.0,
"correct_answers" : 54.0,
"incorect_answers" : 3.0
}
]
}
/* 4 */
{
"_id" : 4.0,
"user_id" : "acollins3",
"first_name" : "Andrew",
"last_name" : "Collins",
"email" : "acollins3@unesco.org",
"status" : "inactive",
"join_date" : "2016-03-06",
"last_login_date" : "2016-09-30 23:59:05 -0400",
"strengths" : [
"analytics research",
"mongo queries",
"normalization"
],
"courses" : [
{
"code" : "CSIS2300",
"total_questions" : 101.0,
"correct_answers" : 37.0,
"incorect_answers" : 64.0
}
]
}
我的查询没有返回任何内容。
db.assign1.aggregate([
{$project:
{_id: 0,
course_code: "$courses.code",
correctAns: {$max: "$courses.correct_answers"}}},
{$match: {"courses.code": "CSIS2300"}},
{$group: {_id: {"code": "$courses.code"}, Average_CorrectAns: {$avg: "$courses.correct_answers"}}}
]);
答案 0 :(得分:0)
由于 $ project 阶段将 $ courses.code 更改为 course_code ,因此在此阶段之后没有具有该名称的字段(相同)适用于 $ courses.correct_answers )。此外,通常首先 $ match 会更好,因为它可以提高查询效果。此外,您可以逐个向查询添加阶段并每次运行它以查看每个阶段后获得的输出。
要查找平均值,您需要展开课程数组,最终查询如下:
db.assign1.aggregate([{$unwind: "$courses"},
{$match: {"courses.code": "CSIS2300"}},
{$group: {_id: {"code": "$courses.code"}, correctAns: {$avg: "$courses.correct_answers"}}},
{$project: {course_Code: "$_id.code", correctAns: 1, _id: 0}}
]);