NAME MooseX::Role::MongoDB - Provide MongoDB connections, databases and collections VERSION version 0.002 SYNOPSIS In your module: package MyData; use Moose; with 'MooseX::Role::MongoDB'; has database => ( is => 'ro', isa => 'Str', required => 1, ); has client_options => ( is => 'ro', isa => 'HashRef', default => sub { {} }, ); sub _build__mongo_default_database { return $_[0]->database } sub _build__mongo_client_options { return $_[0]->client_options } In your code: my $obj = MyData->new( database => 'MyDB', client_options => { host => "mongodb://example.net:27017", username => "willywonka", password => "ilovechocolate", }, ); $obj->mongo_database("test"); # test database $obj->mongo_collection("books"); # in default database $obj->mongo_collection("otherdb" => "books"); # in other database DESCRIPTION This role helps create and manage MongoDB objects. All MongoDB objects will be generated lazily on demand and cached. The role manages a single MongoDB::MongoClient connection, but many MongoDB::Database and MongoDB::Collection objects. The role also compensates for forks. If a fork is detected, the object caches are cleared and new connections and objects will be generated in the new process. When using this role, you should not hold onto MongoDB objects for long if there is a chance of your code forking. Instead, request them again each time you need them. METHODS mongo_database $obj->mongo_database( $database_name ); Returns a MongoDB::Database. The argument is the database name. With no argument, the default database name is used. mongo_collection $obj->mongo_collection( $database_name, $collection_name ); $obj->mongo_collection( $collection_name ); Returns a MongoDB::Collection. With two arguments, the first argument is the database name and the second is the collection name. With a single argument, the argument is the collection name from the default database name. CONFIGURING The role uses several private attributes to configure itself: * "_mongo_client_class" — name of the client class * "_mongo_client_options" — passed to client constructor * "_mongo_default_database" — default name used if not specified Each of these have lazy builders that you can override in your class to customize behavior of the role. The builders are: * "_build__mongo_client_class" — default is "MongoDB::MongoClient" * "_build__mongo_client_options" — default is an empty hash reference * "_build__mongo_default_database" — default is the string 'test' You will generally want to at least override "_build__mongo_client_options" to allow connecting to different hosts. You may want to set it explicitly or you may want to have your own public attribute for users to set (as shown in the "SYNOPSIS"). The choice is up to you. If a MongoDB "host" string is provided in the client options hash, any host names will be converted to IP addresses to avoid known bugs using authentication over SSL. Note that the "_mongo_default_database" is also used as the default database for authentication, unless a "db_name" is provided to "_mongo_client_options". LOGGING This role logs using Log::Any, which by default uses a "Null" logger and discards messages. Currently, only 'debug' level logs messages are generated for tracing MongoDB interactions activity across forks. See the tests for an example of how to enable it. SEE ALSO * Moose * MongoDB SUPPORT Bugs / Feature Requests Please report any bugs or feature requests through the issue tracker at . You will be notified automatically of any progress on your issue. Source Code This is open source software. The code repository is available for public review and contribution under the terms of the license. git clone https://github.com/dagolden/MooseX-Role-MongoDB.git AUTHOR David Golden COPYRIGHT AND LICENSE This software is Copyright (c) 2013 by David Golden. This is free software, licensed under: The Apache License, Version 2.0, January 2004