diff options
| author | HumairAK <humair88@hotmail.com> | 2016-07-24 06:13:31 +0000 |
|---|---|---|
| committer | HumairAK <humair88@hotmail.com> | 2016-07-24 06:13:31 +0000 |
| commit | 6522ff6aa38b5a8d56f64460d1646e4ab7ab8dc2 (patch) | |
| tree | 5e16217dbea4e23fc93413ba6f9d0c317812ec39 | |
| parent | 3e777af0387821e7a7fe03792cf79915d1b20bc7 (diff) | |
| parent | e83ad0a7931611822f49f84bf21a204df1de8eb6 (diff) | |
Merge branch 'master' of https://github.com/HumairAK/solutions_repo
| -rw-r--r-- | node_simple.js | 30 | ||||
| -rw-r--r-- | testImports.js | 5 |
2 files changed, 35 insertions, 0 deletions
diff --git a/node_simple.js b/node_simple.js index b8ada6b..d3eddb4 100644 --- a/node_simple.js +++ b/node_simple.js @@ -15,6 +15,7 @@ * 10. comment_history ? DONE * 11. solutions_history ? DONE * 12. voting for solutions ? DONE + * 23. A search users function ? DONE * * */ @@ -98,6 +99,35 @@ var uri = exports.uri = 'mongodb://general:assignment4@ds057862.mlab.com:57862/ //****************************FUNCTIONS************************************************| +exports.search_users = function ( token, callback ) { + + mongoFactory.getConnection(uri).then(function (db) { + var users = db.collection('users'); + users.createIndex( // make the following fields searchable + { + "user_name":"text", + "f_name":"text", + "l_name":"text" + }); + users.find( + { $text: { $search: token } }, + { score: { $meta: "textScore" } } + ).sort( { score: { $meta:"textScore" } } ).toArray(function (err, docs) { + if (err) callback(false, "Error: some error while searhing"); + else { + // console.log(docs); + callback(true, docs); + } + }); + + db.close(); + + }).catch(function (err) { + console.error(err); + }); +}; + + exports.followExam = function (user_name, exam_id, callback) { exports.retrieveFollows(user_name, function (bool, result) { diff --git a/testImports.js b/testImports.js index 7546f06..6a900e1 100644 --- a/testImports.js +++ b/testImports.js @@ -134,3 +134,8 @@ dbFile.retrieveFollows("some_user names", function (bool, result) { /*dbFile.vote_solution("5792d8a970040378d4e4b389" , "down", function (bool, mssg) { console.log(mssg); });*/ + + +dbFile.search_users("david", function (bool, mssg) { + if (mssg.length != 0) console.log(mssg); +}); |
