user2323174
user2323174

Reputation: 55

Error with MongoDB, Can't locate object method

I want to access at my database in this way:

my $db = MongoDB::Connection->new or die( "erreur de connection" );
db = $db->chasseur_de_tete;

and I get this error:

Can't locate object method "chasseur_de_tete" via package "MongoDB::Connection" at ../AbstractExtract.pm line 25.

I don't understand why, after search for similar cases, I saw that problemme could be caused by not updating modules (like DateTime). but even after update, I get the same error.

Have you some idea please ? Thanks

Upvotes: 0

Views: 711

Answers (1)

Neil Lunn
Neil Lunn

Reputation: 151122

The preferred method is to use the MongoClient class and accessors to databases and collections are not built in, they require separate methods to retrieve.

use MongoDB;

my $client     = MongoDB::MongoClient->new(host => 'localhost', port => 27017);
my $database   = $client->get_database( 'chasseur_de_tete' );
my $collection = $database->get_collection( 'mycollection' );

Also the same code is equivalent for the $client connection:

my $client = MongoDB::MongoClient->new();

Which assumes the default values for the connection.

Upvotes: 3

Related Questions