为什么我的mongodb查询不返回任何内容?
/*我是MongoDB的新手,并试图找出如何获得CSIS2300的平均correct_answers
。为什么我的mongodb查询不返回任何内容?
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" : "[email protected]",
"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" : "[email protected]",
"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" : "[email protected]",
"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" : "[email protected]",
"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"}}}
]);
因为你$项目阶段改变$ courses.code到course_code,所以这个阶段后也没有什么领域使用该名称(同样适用于$ courses.correct_answers)。另外,通常最好先与$匹配,因为它可以提高查询的性能。此外,您可以逐个将查询阶段添加到查询中,并每次运行查看每个阶段后获得的输出。
要查找平均需要$放松身心的课程阵列,最终查询如下:
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}}
]);
我用** course_code ** ** ** ** **而不是** $ courses.code **。我工作的很好,但现在我得到_null_的平均值:( – Cyrille
@Cyrille在你的问题中举一个整个文档的例子 –
@Cyrille或者至少修改现有的例子,以便在阵列中有几个元素。 –
尝试使用$放松,而不是$项目阶段 – felix
我用$放松的“课程”我得到了结果,但我仍然不确定我是否对问题做出了正确回答。 – Cyrille
嗯,只有你可以告诉,因为我们没有数据... – felix