NAME
Mojolicious::Plugin::Mongodb - Use MongoDB in Mojolicious
VERSION
version 1.16
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'
no_db (optional) Don't install the 'db' helper
no_model (optional) Don't install the 'model' helper
no_coll (optional) Don't install the 'coll' helper
no_find_and_modify (optional) Don't install the 'find_and_modify' helper
no_map_reduce (optional) Don't install the 'map_reduce' helper
no_group (optional) Don't install the 'group' helper
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<coll>.
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 http://www.mongodb.org/display/DOCS/findAndModify+Command 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 http://www.mongodb.org/display/DOCS/MapReduce 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.
AUTHOR
Ben van Staveren, <madcat at cpan.org>
BUGS/CONTRIBUTING
Please report any bugs through the web interface at http://github.com/benvanstaveren/mojolicious-plugin-mongodb/issues If you want to contribute changes or otherwise involve yourself in development, feel free to fork the Git repository from https://github.com/benvanstaveren/mojolicious-plugin-mongodb/.
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, 2013 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.