1

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:

  1. 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

  2. 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.

3
  • Perhaps firewall blocks port 27017 on network interface 192.168.10.24. Scan port 27017 on interface 192.168.10.24 from remote host on same network -- result of scan should confirm if the port is filtered out by firewall. Commented Apr 5, 2021 at 1:56
  • Thanks a lot. How to scan port 27017 on interface 192.168.10.24? Commented Apr 5, 2021 at 2:19
  • Read documentation for nmap. Nmap command examples. Commented Apr 6, 2021 at 8:08

1 Answer 1

1

You are storing a local time (time without time zone) in MongoDB but MongoDB only supports UTC timestamps. The warning tells you that the local time is converted to UTC before being stored.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks fro your reply. Any idea how to get rid of the warning message?
What happens you add time zone info to the DateTime object?
I added: $dt->set_time_zone( 'local' ); The waring message disappeared. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.