http://qs321.pair.com?node_id=1106660

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

the problem is i need to generate and validate mongo db quiries, iam writing the below module for validating query,i need to get an error message or some trip condition,the name of the collection acually present is 'student' but if i give 'studentgggggg' its not giving any error message ......please help me out

#!/usr/bin/perl use MongoDB; use JSON; use Data::Dumper; my $conn = MongoDB::Connection->new( host => 'xxxxxxx', port => 27017, username=>'xxxx', password=>'xxxx', db_name => 'prav' ); my $database = $conn->get_database( 'prav' ); my $collection = $database->get_collection( 'studenttgggggg'); my $err = $database->last_error(); print Dumper $err; my $data = $collection->find({"id"=>"stu_7"}); my $err = $collection->last_error(); # print %$err; print Dumper $err; #print Dumper $data; while (my $doc = $data->next) { my @qual=@{$doc->{'qualifications'}}; print @qual; print "\n"; }

Replies are listed 'Best First'.
Re: error catching in mongodb and perl
by Loops (Curate) on Nov 10, 2014 at 06:05 UTC

    Hi there,

    From the docs:

    NOTE: MongoDB::Connection is DEPRECATED as of version 0.502.0 of the MongoDB CPAN distribution. It is no longer maintained and will be removed in a future version. Use MongoDB::MongoClient instead.

    But having looked a little closer, the MongoDB::Tutorial is still using the Connection object rather than MongoClient. And doesn't show any error checking...

      i got an error while using MogoDB::MongoClient as below,i tried to install using cpan but its not istalling.

      use MongoDB; use JSON; use Data::Dumper; $client = MongoDB::MongoClient->new( host => 'remote_machine_ip:270 +17'); $client->connect; $client->authenticate('username', 'password', 'authenticationdb'); my $database = $client->get_database('prav'); my $collection = $database->get_collection( 'student'); my $data=$collection->find(); while (my $doc = $data->next) { my @qual=@{$doc->{'qualifications'}}; print @qual; print "\n"; } Can't locate object method "new" via package "MongoDB::MongoClient" (p +erhaps you forgot to load "MongoDB::MongoClient"?) at .. cpan MongoDB::MongoClient ---also tried cpan MongoDB .... .... Running make test Can't test without successful make Running make install Make had returned bad status, install seems impossible

        What was the error when trying to install MongoDB::MongoClient? That appears to be the crux of the matter.

        You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.