MongoDB::Connection - A connection to a Mongo server
version 0.27
The MongoDB::Connection class creates a connection to the MongoDB server.
By default, it connects to a single server running on the local machine listening on the default port:
# connects to localhost:27017 my $connection = MongoDB::Connection->new;
It can connect to a database server running anywhere, though:
my $connection = MongoDB::Connection->new(host => 'example.com', port => 12345);
It can also be used to connect to a replication pair of database servers:
my $connection = MongoDB::Connection->new(left_host => '192.0.2.0', right_host => '192.0.2.1');
If ports aren't given, they default to 27017.
27017
Hostname to connect to. Defaults to localhost.
localhost
Port to use when connecting. Defaults to 27017.
Paired connection host to connect to. Can be master or slave.
Port to use when connecting to left_host. Defaults to 27017.
Port to use when connecting to right_host. Defaults to 27017.
Boolean indicating whether or not to reconnect if the connection is interrupted. Defaults to 1.
1
Boolean indication whether or not to connect automatically on object construction. Defaults to 1.
$connection->connect;
Connects to the mongo server. Called automatically on object construction if auto_connect is true.
auto_connect
my @dbs = $connection->database_names;
Lists all databases on the mongo server.
my $database = $connection->get_database('foo');
Returns a MongoDB::Database instance for database with the given $name.
MongoDB::Database
$name
$master = $connection->find_master
Determines which host of a paired connection is master. Does nothing for a non-paired connection. This need never be invoked by a user, it is called automatically by internal functions. Returns values:
The left host is master
The right host is master
Error, master cannot be determined.
$connection->authenticate('foo', 'username', 'secret');
Attempts to authenticate for use of the $dbname database with $username and $password. Passwords are expected to be cleartext and will be automatically hashed before sending over the wire, unless $is_digest is true, which will assume you already did the hashing on yourself.
$dbname
$username
$password
$is_digest
my ($insert, $ids) = MongoDB::write_insert('foo.bar', [{name => "joe", age => 40}]); $conn->send($insert);
Low-level function to send a string directly to the database. Use MongoDB::write_insert, MongoDB::write_update, MongoDB::write_remove, or MongoDB::write_query to create a valid string.
my $cursor = $conn->recv({ns => "foo.bar"});
Low-level function to receive a response from the database. Returns a MongoDB::Cursor. At the moment, the only required field for $info is "ns", although "request_id" is likely to be required in the future. The $info hash will be automatically created for you by MongoDB::write_query.
MongoDB::Cursor
$info
Kristina Chodorow <kristina@mongodb.org>
1 POD Error
The following errors were encountered while parsing the POD:
Expected text after =item, not a number
To install MongoDB, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MongoDB
CPAN shell
perl -MCPAN -e shell install MongoDB
For more information on module installation, please visit the detailed CPAN module installation guide.