praveenchappa has asked for the wisdom of the Perl Monks concerning the following question:

I am tryinh to connet to mongo db on a remote machine and query on a database its working fine but i want get the status code of the query wheter it is executed or any syntax errors..

my $conn = MongoDB::Connection->new( host => 'hostname', port => 27017, username=>'xxxx', password=>'xxxx', db_name => 'xxxx' ); my $database = $conn->get_database( 'xxxx' ); my $collection = $database->get_collection( 'student' ); my $data = $collection->find({"id"=>"stu_7";}); #i want to check exit or status code here !!!!!!! #...... while (my $doc = $data->next) { my @qual=@{$doc->{'qualifications'}}; print @qual; }

Replies are listed 'Best First'.
Re: how to capture status codes of mongodb in perl
by blindluke (Hermit) on Nov 05, 2014 at 11:09 UTC

    Look at the docs, they are quite detailed. In particular, the docs for the MongoDB::Database object show that there is a last_error method, and you can probably do this:

    my $err = $database->last_error();

    to get what you want.

    - Luke

      i have tried that,but a hash code is prinnted while trying to print that

      my $err = $database->last_error(); print $err; #prints: HASH(0x2ad39fc)

      how could i validate that

        You can dereference the reference
        print %$err;

        or, better, use Data::Dumper:

        use Data::Dumper; print Dumper $err;
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        What have you tried? Have you tried looking at the documentation, and following the link I posted? If you tried, then try reading this particular fragment again:

        last_error returns a hash with fields that vary, depending on what the previous operation was and if it succeeded or failed. If the last operation (before the last_error call) failed, either:
        err will be set or
        errmsg will be set and ok will be 0.

        If err is null and ok is 1, the previous operation succeeded.

        - Luke