MongoDBx::Class::Connection(3) A connection to a MongoDB server


version 1.030002




# connect to a MongoDB server
my $conn = $mongodbx->connect(host => '', port => 27017);
# the connection object is automatically saved to the 'conn'
# attribute of the L<MongoDBx::Class> object (C<$mongodbx> above)


MongoDBx::Class::Connection extends MongoDB::Connection. This class provides the document expansion and collapsing methods that are used internally by other MongoDBx::Class classes.

Note that a MongoDBx::Class object can only have one connection at a time. Connection is only made via the "connect()" method in MongoDBx::Class.


Aside for attributes provided by MongoDB::Connection, the following special attributes are added:


A string representing the namespace of document classes to load (e.g. MyApp::Schema). This is a required attribute (automatically received from the MongoDBx::Class object).


A hash-ref of document classes loaded. This is a required attribute (automatically received from the MongoDBx::Class object).


A boolean value indicating whether to use safe operations (e.g. inserts and updates) by default - without the need to pass "{ safe => 1 }" to relevant methods - or not. False by default.


This boolean attribute is used by MongoDBx::Class::ConnectionPool objects that use a backup connection.


Aside from the methods provided by MongoDB::Connection, the following methods and modifications are added:

get_database( $name )

Returns a MongoDBx::Class::Database object representing the MongoDB database named $name.

safe( $boolean )

Overrides the current value of the safe attribute with a new boolean value.

expand( $coll_ns, \%doc )

Receives the full name (a.k.a namespace) of a collection (that is the database name, followed by a dot, and the collection name), and a document hash-ref, and attempts to expand it according to the '_class' attribute that should exist in the document. If it doesn't exist, the document is returned as is.

This is mostly used internally and you don't have to worry about expansion, it's done automatically.

collapse( \%doc )

Receives a document hash-ref and returns a collapsed version of it such that it can be safely inserted to the database. For example, you can't save an embedded document directly to the database, you need to convert it to a hash-ref first.

This method is mostly used internally and you don't have to worry about collapsing, it's done automatically.


_collapse_val( $val )


Ido Perlmuter, "<ido at>"


Please report any bugs or feature requests to "bug-mongodbx-class at", or through the web interface at <>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


You can find documentation for this module with the perldoc command.

        perldoc MongoDBx::Class::Connection

You can also look for information at:


Copyright 2010-2014 Ido Perlmuter.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See for more information.