public-misc-perlscript-read_data_from_Mongo_DB


 

#To install perl mongodb module in Ubuntu machine:
#sudo apt-get update
#sudo apt-get install libmongodb-perl
#
#To search for a package available in Ubuntu : http://packages.ubuntu.com/(search package directories – enter keyword)
#
#!/usr/bin/perl
use MongoDB;
use MongoDB::Connection;
use Data::Dumper;
my $hostname = "localhost";
my $port = 27017;
my $conn = MongoDB::Connection->new( "host" => "$hostname",
                                     "port" => $port );
#my $db = $conn->get_database( 'test' );
#my $user_stats = $db->get_collection( 'test_stats' );
my ($db,$user_stats);
main ();
sub main {
   processInput();
   useData();
}


sub useData {
   $db = $conn->get_database( "$dataBase" );
   $user_stats = $db->get_collection( "$collName1" );

 # Read documents of a collection in a db
   my $cursor = $user_stats->find();
   open(my $fh, '>',"$file");

   print $fh "\$(function(){ \n";
   print $fh "  var destinations = [ \n";
   print $fh "    { value: \"Agra, Uttar Pradesh\", suburl: \"\", data: \"agra\" },\n";
   print $fh "    { value: \"Bengaluru, Karnataka\", suburl: \"\", data: \"bengaluru\" },\n";
   print $fh "    { value: \"Chennai, Tamil Nadu\", suburl: \"\", data: \"chennai\" },\n";
   print $fh "    { value: \"Delhi\", suburl: \"\", data: \"delhi\" },\n";
   print $fh "    { value: \"Goa\", suburl: \"\", data: \"goa\" },\n";
   print $fh "    { value: \"Gurgaon, Haryana\", suburl: \"\", data: \"gurgaon\" },\n";
   print $fh "    { value: \"Hyderabad, Andhra Pradesh\", suburl: \"\", data: \"hyderabad\" },\n";
   print $fh "    { value: \"Jaipur, Rajasthan\", suburl: \"\", data: \"jaipur\" },\n";
   print $fh "    { value: \"Kolkata, West Bengal\", suburl: \"\", data: \"kolkata\" },\n";
   print $fh "    { value: \"Mumbai, Maharashtra\", suburl: \"\", data: \"mumbai\" },\n";
   print $fh "    { value: \"Noida, Uttar Pradesh\", suburl: \"\", data: \"noida\" },\n";
   print $fh "    { value: \"Kashmir\", suburl: \"\", data: \"kashmir\" },\n";
   print $fh "    { value: \"Puri, Odisha\", suburl: \"\", data: \"puri\" },\n";
   print $fh "    { value: \"Bhubaneshwar, Odisha\", suburl: \"\", data: \"bhubaneshwar\" },\n";
   print $fh "    { value: \"Sikkim\", suburl: \"\", data: \"sikkim\" },\n";
   print $fh "    { value: \"Indore, Madhya Pradesh\", suburl: \"\", data: \"indore\" },\n";
   print $fh "    { value: \"Pune, Maharashtra\", suburl: \"\", data: \"pune\" },\n";
   print $fh "    { value: \"Ahmedabad, Gujarat\", suburl: \"\", data: \"ahmedabad\" },\n";
   print $fh "    { value: \"Guwahati, Assam\", suburl: \"\", data: \"guwahati\" },\n";
   print $fh "    { value: \"Chandigarh\", suburl: \"\", data: \"chandigarh\" },\n";
   print $fh "    { value: \"Varanasi, Uttar Pradesh\", suburl: \"\", data: \"varanasi\" },\n";
   print $fh "    { value: \"Allahabad, Uttar Pradesh\", suburl: \"\", data: \"allahabad\" },\n";
   print $fh "    { value: \"Bhopal, Madhya Pradesh\", suburl: \"\", data: \"bhopal\" },\n";
   print $fh "    { value: \"Ranchi, Jharkhand\", suburl: \"\", data: \"ranchi\" },\n";
   print $fh "    { value: \"Patna, Bihar\", suburl: \"\", data: \"patna\" },\n";
   print $fh "    { value: \"Andaman and Nicobar Islands, India\", suburl: \"\", data: \"andaman\" },\n";
   print $fh "    { value: \"Kerala, India\", suburl: \"\", data: \"kerala\" },\n";
   print $fh "    { value: \"Lakshadweep Islands, India\", suburl: \"\", data: \"lakshadweep\" },\n";

   while (my $obj = $cursor->next){
        print $fh "    { value: \"$obj->{name}\", suburl: \"venues\/\", data: \"$obj->{_id}\" },\n";
   };
   close ($fh);


   open(my $fh, '>>',"$file");
   $user_stats = $db->get_collection( "$collName2" );

 # Read documents of a collection in a db
   my $cursor = $user_stats->find();

   while (my $obj = $cursor->next){
        print $fh "    { value: \"$obj->{name}\", suburl: \"subList\/\", data: \"$obj->{_id}\" },\n";
   };

   print $fh "  ]; \n";
   print $fh "  \$('#autocomplete').autocomplete({ \n";
   print $fh "    lookup: destinations,\n";
   print $fh "    autoSelectFirst: true,\n";
   print $fh "    onSelect: function (suggestion) {\n";
   print $fh "      var thehtml = suggestion.suburl + suggestion.data; \n";
   print $fh "      window.open(\"https://www.spotdekho.com/\" + thehtml,\"_self\"); \n";
   print $fh "    }\n";
   print $fh "   });\n ";
   print $fh "});\n ";
   close ($fh);

  # print Dumper($myStr);
}

sub processInput {

    my $arg = "";
    my $opt = "";
    if ($#ARGV == -1) {
       printusage();
     }
    while ($#ARGV != -1) {
        $arg = join(" ", @ARGV);
        $opt = shift(@ARGV);
        if ($opt =~ /^-h(elp)?$/){
           printusage();
        } elsif ($opt =~ /^-file/) {
           if (($#ARGV != -1) && ($ARGV[0] !~ /^-/)) {
               $file = shift(@ARGV);
           } else {
             print "option $opt requires atleast one argument\n";
             exit;
           }
        } elsif ($opt =~ /^-db/) {
           if (($#ARGV != -1) && ($ARGV[0] !~ /^-/)) {
               $dataBase = shift(@ARGV);
           } else {
             print "option $opt requires atleast one argument\n";
             exit;
           }
        } elsif ($opt =~ /^-collec1/) {
           if (($#ARGV != -1) && ($ARGV[0] !~ /^-/)) {
               $collName1 = shift(@ARGV);
           } else {
             print "option $opt requires atleast one argument\n";
             exit;
           }
        } elsif ($opt =~ /^-collec2/) {
           if (($#ARGV != -1) && ($ARGV[0] !~ /^-/)) {
               $collName2 = shift(@ARGV);
           } else {
             print "option $opt requires atleast one argument\n";
             exit;
           }
        } else {
             print "\nINVALID OPTION!\n\n";
             printusage();
        }
    }
}


sub printusage() {
        print "\n\nUSAGE : $0 -file <File_name with file path> -db <database name> -collection <Collection name of the db> -h[elp]\n";
        print "\n-file\t:\tFile_name with file path ex: /home/webapp/perlscript/filename ";
        print "\n-db\t:\tDatabase name from which data will be read, ex : test \n";
        print "\n-collec1\t:\tFirst Collection name in the db from where data will be read, ex: venue \n";
        print "\n-collec2\t:\tSecond Collection name in the db from where data will be read, ex: venue \n";
        print "For Example, the following command :\n";
        print "sudo \t$0 -file /home/sushil/spotOnGit/public/javascripts/lib/searchAutocomplete.js -db spotdekho -collec1 venue -collec2 subList \n";
        exit;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *