Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. var fs = require('fs');
  2. var mc = require('mongodb').MongoClient;
  3.  
  4. /*Parsing the command line arguments*/
  5. var count = 0;
  6. var output = '';
  7. var maxCount = '';
  8. var queryString = '';
  9. var queryObj = {};
  10. var projectionString = '';
  11. var projectionObj = {};
  12.  
  13. if (process.argv[2].substr(0,8) === '--output') {
  14. output = process.argv[2].split('=')[1]; //output = output count = 2
  15. count++;
  16. }
  17. if (process.argv[2+count].substr(0,10) === '--maxcount') {
  18. maxCount = parseInt(process.argv[2+count].split('=')[1]); //maxcount = number count =2
  19. count++;
  20. }
  21.  
  22. queryString = JSON.stringify(process.argv[4].split('=')[1]); //querystring = process 4
  23. if (queryString[0] != '-') {
  24. queryObj = JSON.parse('' + queryString + '');
  25. console.log(queryObj);
  26.  
  27. }
  28. else {
  29. queryObj = JSON.parse(queryString);
  30. console.log(queryObj);
  31. }
  32.  
  33.  
  34. if (process.argv.length === 4+count) { //projection = process 5
  35. projectionString = JSON.stringify(process.argv[3+count].split('=')[1]);
  36. if (projectionString[0] != '{') {
  37. projectionObj = JSON.parse('' + projectionString + '');
  38. console.log(projectionObj);
  39. }
  40. else {
  41. projectionObj = JSON.parse(projectionString);
  42. console.log(projectionObj);
  43. }
  44. }
  45.  
  46. /* end argument parsing */
  47.  
  48. /*A short function to check if arguments exist*/
  49. var argExists = function(arg) {
  50. return arg != '';
  51. }
  52.  
  53. /*Connect to the persistent-logs database*/
  54. mc.connect('mongodb://localhost/log-demo', function(err, db) {
  55.  
  56. var toArrayCallback = function(err, data) {
  57. if (argExists(output)) {
  58. fs.writeFile(output, JSON.stringify(data));
  59. }
  60. else {
  61. console.log(JSON.stringify(data));
  62. }
  63. process.exit(0);
  64. }
  65.  
  66. var logCollection = db.collection('logs');
  67.  
  68. if (argExists(maxCount)) {
  69. logCollection.find(queryObj, projectionObj).limit(maxCount).toArray(toArrayCallback);
  70. }
  71. else {
  72. logCollection.find(queryObj, projectionObj).toArray(toArrayCallback);
  73. }
  74.  
  75.  
  76. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement