From ac00f848b0c971abb8d944f1cead430baa834b06 Mon Sep 17 00:00:00 2001 From: Damani Pradyumn Date: Tue, 19 Jul 2016 23:03:38 -0400 Subject: working on queries --- node_simple.js | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 121 insertions(+), 5 deletions(-) (limited to 'node_simple.js') diff --git a/node_simple.js b/node_simple.js index 5542c40..894d63b 100644 --- a/node_simple.js +++ b/node_simple.js @@ -13,6 +13,8 @@ * */ +/* I pass in exam id and you give me the number of questions and the comments and solutions associated with each question?*/ + /* Tables SO FAR: * 1. exams * 2. courses @@ -36,10 +38,10 @@ // |..........| -// |================================solutions===================================| -// |_________ _id_____________|exam_id_____________________|q_id_|text____|votes| -// |==========================|============================|=====|========|=====| -// |"354ff71ed078933079d6467e"|"578a44ff71ed097fc3079d6e" |1 |"answer"| 1 | +// |================================solutions============================================| +// |_________ _id_____________|exam_id_____________________|q_id_|text____|votes|comments| +// |==========================|============================|=====|========|=====|========| +// |"354ff71ed078933079d6467e"|"578a44ff71ed097fc3079d6e" |1 |"answer"| 1 |[{},{}] | // |..........| var exports = module.exports = {}; @@ -64,6 +66,119 @@ 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 + + + + mongoFactory.getConnection(uri) + .then(function (db) { + + var solutions = db.collection('solutions'); + + var cursor = solutions.find({ exam_id: exam_id}), i = 0; + + var count = []; + cursor.forEach(function (x) { + + console.log(x); + }); + + +/* var q_count = []; + + for (var i = 1; i <= 2; i++) { + solutions.count( + { + exam_id: exam_id, + q_id: i + } + ,function (err, result) { + q_count.push(result); + console.log(q_count); + + // console.log("found "+ result + " solutions"); + } + ); + }*/ + + + }) + .catch(function (err) { + console.err(err); + }) + +} + + + + + + + + + +exports.add_comment = function (sol_id, fields) { + var Data = { + text: fields[0], + date: fields[1], + by: fields[2] + }; + + mongoFactory.getConnection(uri) + .then(function (db) { + + // find the solutions table + var solutions = db.collection('solutions'); + // insert data into table + solutions.updateOne( {_id: ObjectId(sol_id)}, {$push: {comments: Data}} , function (err, result) { + if (err) throw err; + else { + console.log("comment added"); + } + }); + + }) + .catch(function (err) { + console.error(err); + }) +} + +// get all solutions given an exam_id and the question number +exports.get_all_solutions = function (exam_id, q_num) { + mongoFactory.getConnection(uri) + .then(function (db) { + + var solutions = db.collection('solutions'); + + solutions.find( + { + exam_id: exam_id, + q_id: q_num + } + ).toArray( function (err, docs) { + if (err) throw err; + else { + console.log(docs); + } + }); + + + }) + .catch(function () { + console.error(err); + }) + + +} + /* * This function will add a solution to the solutions table in the database . * Params: fields - [exam_id , question_id, solution text] @@ -76,7 +191,8 @@ exports.add_solution = function (fields) { exam_id: fields[0], q_id: fields[1], text: fields[2], - votes: 0 + votes: 0, + comments: [] }; // establish a connection -- cgit v1.2.3 From 26a31332f980dd89fd9016e950c8ef05948a0886 Mon Sep 17 00:00:00 2001 From: kumar Date: Wed, 20 Jul 2016 01:28:17 -0400 Subject: added getting number of solutions and comments --- node_simple.js | 68 ++++++++++++++++++++++++++-------------------------------- 1 file changed, 30 insertions(+), 38 deletions(-) (limited to 'node_simple.js') 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); } }); -- cgit v1.2.3