Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var fs = require('fs');
- var mc = require('mongodb').MongoClient;
- /*Parsing the command line arguments*/
- var count = 0;
- var output = '';
- var maxCount = '';
- var queryString = '';
- var queryObj = {};
- var projectionString = '';
- var projectionObj = {};
- if (process.argv[2].substr(0,8) === '--output') {
- output = process.argv[2].split('=')[1]; //output = output count = 2
- count++;
- }
- if (process.argv[2+count].substr(0,10) === '--maxcount') {
- maxCount = parseInt(process.argv[2+count].split('=')[1]); //maxcount = number count =2
- count++;
- }
- queryString = JSON.stringify(process.argv[4].split('=')[1]); //querystring = process 4
- if (queryString[0] != '-') {
- queryObj = JSON.parse('' + queryString + '');
- console.log(queryObj);
- }
- else {
- queryObj = JSON.parse(queryString);
- console.log(queryObj);
- }
- if (process.argv.length === 4+count) { //projection = process 5
- projectionString = JSON.stringify(process.argv[3+count].split('=')[1]);
- if (projectionString[0] != '{') {
- projectionObj = JSON.parse('' + projectionString + '');
- console.log(projectionObj);
- }
- else {
- projectionObj = JSON.parse(projectionString);
- console.log(projectionObj);
- }
- }
- /* end argument parsing */
- /*A short function to check if arguments exist*/
- var argExists = function(arg) {
- return arg != '';
- }
- /*Connect to the persistent-logs database*/
- mc.connect('mongodb://localhost/log-demo', function(err, db) {
- var toArrayCallback = function(err, data) {
- if (argExists(output)) {
- fs.writeFile(output, JSON.stringify(data));
- }
- else {
- console.log(JSON.stringify(data));
- }
- process.exit(0);
- }
- var logCollection = db.collection('logs');
- if (argExists(maxCount)) {
- logCollection.find(queryObj, projectionObj).limit(maxCount).toArray(toArrayCallback);
- }
- else {
- logCollection.find(queryObj, projectionObj).toArray(toArrayCallback);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement