diff options
| -rw-r--r-- | node_simple.js | 68 | ||||
| -rw-r--r-- | testImports.js | 16 |
2 files changed, 42 insertions, 42 deletions
diff --git a/node_simple.js b/node_simple.js index 894d63b..33e9862 100644 --- a/node_simple.js +++ b/node_simple.js @@ -67,49 +67,45 @@ var uri = 'mongodb://general:assignment4@ds057862.mlab.com:57862/solutions_repo' - -exports.get_exam_info_by_ID = function (exam_id) { - - //first get this exam; - // var exam = exports.get_exam_byID(exam_id); - // var num_questions = exam[0].questions_count; - - //get how many solutions there are for each question - - +// this function returns an array where is element contains info for a particular question +// such as the question number (_id), number of solutions (count), and number of comments +// (comments). [ {_id,count,comments}, {} ...] +exports.get_exam_info_by_ID = function (exam_id, callback) { mongoFactory.getConnection(uri) .then(function (db) { var solutions = db.collection('solutions'); - var cursor = solutions.find({ exam_id: exam_id}), i = 0; + solutions.aggregate( + [ - var count = []; - cursor.forEach(function (x) { - - console.log(x); - }); - - -/* var q_count = []; - - for (var i = 1; i <= 2; i++) { - solutions.count( + // {$unwind: "$comments"}, + { $match: { exam_id: exam_id }}, { - exam_id: exam_id, - q_id: i - } - ,function (err, result) { - q_count.push(result); - console.log(q_count); + $project: + { + num_comments: { $size: "$comments" }, + _id: "$exam_id", + q_id: "$q_id" + } + }, + { + $group : { + _id : "$q_id", + count: { $sum: 1 }, + comments: {$sum: "$num_comments"} + // num_comments: { $size: "$comments" } - // console.log("found "+ result + " solutions"); + } } - ); - }*/ + ]).toArray(function (err, result) { + // console.log(result); + callback(result); + + }); }) .catch(function (err) { console.err(err); @@ -119,12 +115,6 @@ exports.get_exam_info_by_ID = function (exam_id) { - - - - - - exports.add_comment = function (sol_id, fields) { var Data = { text: fields[0], @@ -152,7 +142,7 @@ exports.add_comment = function (sol_id, fields) { } // get all solutions given an exam_id and the question number -exports.get_all_solutions = function (exam_id, q_num) { +exports.get_all_solutions = function (exam_id, q_num, callback) { mongoFactory.getConnection(uri) .then(function (db) { @@ -167,6 +157,8 @@ exports.get_all_solutions = function (exam_id, q_num) { if (err) throw err; else { console.log(docs); + + callback(docs); } }); diff --git a/testImports.js b/testImports.js index f64f984..c9f0bb2 100644 --- a/testImports.js +++ b/testImports.js @@ -30,10 +30,10 @@ var questions_array = ["this is q1", "this is q2"]; // dbFile.add_course("CSC148", "Intro to Programming"); //test adding solution -//dbFile.add_solution(["578a44ff71ed097fc3079d6e", 1, "this is yet another solution to q1"]); - +//dbFile.add_solution(["578a44ff71ed097fc3079d6e", 2, "this is a solution to q2"]); +// the following just prints the exam to console // dbFile.get_exam_byID("578a44ff71ed097fc3079d6e"); //test getting all solutions of a given exam and question number @@ -41,6 +41,14 @@ var questions_array = ["this is q1", "this is q2"]; //test adding of comments given a solution_id, and the comment information as an array -// dbFile.add_comment("578edba3e5611f0d4e23c65f", ["this is the 2nd comment", "this is the date", "this is the author"]); +// dbFile.add_comment("578f08e43bba452ee98da444", ["this is asdfasdf", "this is the date", "this is the author"]); + -dbFile.get_exam_info_by_ID("578a44ff71ed097fc3079d6e");
\ No newline at end of file +dbFile.get_exam_info_by_ID("578a44ff71ed097fc3079d6e", function (result) { + if (result.length == 0) { + console.log("some error occured"); + } + else { + console.log(result); + } +});
\ No newline at end of file |
