NAME Mojolicious::Plugin::Mongodb - Use MongoDB in Mojolicious VERSION version 1.13 SYNOPSIS Provides a few helpers to ease the use of MongoDB in your Mojolicious application. use Mojolicious::Plugin::Mongodb sub startup { my $self = shift; $self->plugin('mongodb', { host => 'localhost', port => 27017, helper => 'db', }); } CONFIGURATION OPTIONS helper (optional) The name to give to the easy-access helper if you want to change it's name from the default 'db' patch_mongodb (optional) When set to true, will do some Moose tricks on MongoDB::Collection to force the group, find_and_modify and map_reduce methods in there All other options passed to the plugin are used to connect to MongoDB. HELPERS/ATTRIBUTES mongodb_connection This plugin helper will return the MongoDB::Connection object, use this if you need to access it for some reason. db([$dbname]) This is the name of the default easy-access helper. The default name for it is 'db', unless you have changed it using the 'helper' configuration option. This helper will return either the database you specify, or the last database you specified. You must use this in order to set the database you wish to operate on for those helpers that specify you should. sub someaction { my $self = shift; # select a database $self->db('my_snazzy_database')->get_collection('foo')->insert({ bar => 'baz' }); # do an insert on the same database $self->db->get_collection('foo')->insert({ bar => 'baz' }); } coll($collname) This helper allows easy access to a collection. It requires that you have previously selected a database using the 'db' helper. It will return undef if you have not specified a database first. sub someaction { my $self = shift; # get the 'foo' collection in the 'bar' database $self->db('bar'); my $collection = $self->coll('foo'); # get the 'bar' collection in the 'baz' database $self->db('baz'); my $collection = $self->coll('bar'); } model($db_and_collection) This helper functions as a combination of the above, or if you just want to use a different notation. An example usage would be: # get the number of items in collection 'bar' in database 'foo' my $count = $self->model('foo.bar')->count(); # if you use dotted collection names, no problem! my $system_js_count = $self->model('foo.system.js')->count(); # if you pass it a regular string without dots, this helper will act like C. my $bar_collection = $self->model('bar'); This helper will set the last selected db to whatever was passed as the database part of the argument, so try not to mix and match "model" with "coll" and "db". find_and_modify($collname, \%options) This helper executes a 'findAndModify' operation on the given collection. You must have selected a database using the 'db' helper. See for supported options. It will return the raw result from the MongoDB driver. map_reduce($collname, \%options) This helper executes a 'mapReduce' operation on the given collection. You must have selected a database using the 'db' helper. All options from are supported. It will return undef on failure. On success, it will return the raw result from the MongoDB driver, or if you have passed the 'as_cursor' option, it will return a MongoDB::Cursor object for your result collection. MONGODB PATCHING You can pass an option that will use some Moose tricks to add a few often used (and for some reason inexplicably not included) commands into MongoDB::Collection. You can then use them as follows: my $db = $self->db('foo'); my $coll = $db->get_collection('baz'); $coll->find_and_modify(...); $coll->group(...); $coll->map_reduce(...); find_and_modify(\%options) Executes a 'findAndModify' command on the collection. See L for supported options. It will return the raw result from MongoDB. map_reduce (\%options) Executes a 'mapReduce' operation on the collection. All options from L are supported. It will return the raw result from MongoDB. group (\%options) Executes a 'group' operation on the collection. All options from L are supported. It will return the raw result from MongoDB. AUTHOR Ben van Staveren, "" BUGS/CONTRIBUTING Please report any bugs through the web interface at If you want to contribute changes or otherwise involve yourself in development, feel free to fork the Git repository from . SUPPORT You can find documentation for this module with the perldoc command. perldoc Mojolicious::Plugin::Mongodb You can also look for information at: * AnnoCPAN: Annotated CPAN documentation * CPAN Ratings * Search CPAN ACKNOWLEDGEMENTS Based on Mojolicious::Plugin::Database because I don't want to leave the MongoDB crowd in the cold. Thanks to Henk van Oers for pointing out a few errors in the documentation, and letting me know I should really fix the MANIFEST LICENSE AND COPYRIGHT Copyright 2011, 2012 Ben van Staveren. 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 http://dev.perl.org/licenses/ for more information.