I am using perl mongodb module to connect to mongdb and pull the records from collection Account after a certain time such as '2020-07-17 16:15:16'.
The collection Account is like:
{
"_id" : ObjectId("5f0e13e475a2f6784114b68t”),
"subscriber" : “1234567890”,
"time_stamp" : "2020-07-14T20:21:53Z",
"time" : ISODate("2020-07-14T20:21:53.407Z")
}
My code is:
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Data::Dumper;
use DateTime;
use JSON;
use MongoDB;
my $client = MongoDB::MongoClient->new( host => '127.0.0.1', port => 27017 );
my $db_name = 'test';
my $database = $client->get_database($db_name);
my $collection = $database->get_collection('account');
my $start_date = '2020-07-17 16:15:16';
my $dt_start= convert_string_datetime($start_date);
foreach my $e ($collection->find( {time => { '$gte' => $dt_start }}) ->all) {
my $subscriber = $e->{subscriber};
print "sub=$subscriber \n";
}
exit(0);
sub convert_string_datetime
{
my $str =$_[0];
my $year;
my $four_digit_year;
my $month;
my $day;
my $day_and_time;
my $time;
my $mins;
my $hrs;
my $secs;
($four_digit_year,$month,$day_and_time)=split(/-/,$str);
($day,$time)=split(/ /,$day_and_time); #split the date and time
($hrs,$mins,$secs)=split(/:/,$time);
my $dt = DateTime->new(
year => $four_digit_year,
month => $month,
day => $day,
hour => $hrs,
minute => $mins,
second => $secs,
);
return $dt;
}
I have a couple of questions:
I used the subroutine to convert the time string to the datetime which can be used to compare "time" : ISODate in the collection Account. when I run the above code, it correctly pull the records I want but give a warning error. I don't know how to fix it:
saving floating timezone as UTC at /usr/lib64/perl5/vendor_perl/MongoDB/BSON.pm line 218
The above code works fine to connect to the localhost. But it fails when I try to connect to a remote host to access mongodb within same network such as 192.168.x.x such as:
my $client = MongoDB::MongoClient->new( host => '192.168.10.24', port => 27017 );
What is the best way to connect to a remote host within same 192.168* network?
Thanks a lot for your help.