[英]MongoDB - How to get the course which has the max correct ansewrs (as percentage of total questions) for the active users
MongoDB-如何獲得對活動用戶具有最大正確答案(占總問題百分比)的課程。 文件的一部分。
<code>
{
"_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
}
]
}
</code>
有我的查詢。 我得到了一些結果,但我想確保它是正確的。
<code>
db.assign1.aggregate([
{$unwind: "$courses"},
{$project:
{_id: 0,
user_id: 1,
max_cor_ans: {$max: "$courses.correct_answers"},
status: 1,
course_code: "$courses.code"}
},
{$match: {"status": "active"}},
{$sort: {"max_cor_ans": -1}}
]);
</code>
我認為您的查詢需要一些更改,因為它不會提供您想要的內容,無需執行任何項目階段,請嘗試執行此操作
db.assign1.aggregate([
{$match: {"status": "active"}},
{$unwind: "$courses"},
{$sort : {"courses.correct_answers" : - 1}}
{
$group:
{_id: null,
user_id: {$first : "$user_id"},
max_cor_ans: {$first: "$courses.correct_answers"},
status: {$first : "$status"},
course_code: {"$first : "$courses.code""}
}
}
]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.