The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Mojolicious::Plugin::Mongodb - Use MongoDB in Mojolicious

VERSION

Version 0.02

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,
            database => 'default_database',
            helper => 'db',
            });
    }

CONFIGURATION OPTIONS

All options passed to the plugin are used to connect to MongoDB, with the exception of the optional 'database' argument; if you pass the 'database' argument, this will be your default database which you will access using the helper you specified. The default name for the helper is 'db'.

HELPERS/METHODS

connection

This plugin attribute holds the MongoDB::Connection object, use this if you need to access it for some reason.

db([dbname]) (or if you've given it another name using the 'helper' argument to the plugin method, use that)

This helper will return the database you specify, if you don't specify one, then the default database is returned. If no default has been set and you have not specified a database name, undef will be returned.

    sub someaction {
        my $self = shift;

        # select a database yourself
        $self->db('my_snazzy_database')->get_collection('foo')->insert({ bar: 'baz' });

        # if you passed 'my_snazzy_database' during plugin load as the default, this is equivalent:
        $self->db->get_collection('foo')->insert({ bar: 'baz' });

        # if you want to be anal retentive about things in case no default exists and no database was passed:
        $self->db and $self->db->get_collection('foo')->insert({ bar: 'baz' });

    }

coll(collname, [dbname])

This helper allows easy access to a collection. If you don't pass the dbname argument, it will return the given collection inside the default database.

    sub someaction {
        my $self = shift;

        # get the 'foo' collection in the default database
        my $collection = $self->coll('foo');

        # get the 'bar' collection in the 'baz' database
        my $collection = $self->coll('bar', 'baz');
    }

AUTHOR

Ben van Staveren, <madcat at cpan.org>

BUGS

Please report any bugs or feature requests to bug-mojolicious-plugin-mongodb at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mojolicious-Plugin-Mongodb. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

CONTRIBUTING

If you want to contribute changes or otherwise involve yourself in development, feel free to fork the Mercurial repository from http://bitbucket.org/xirinet/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:

ACKNOWLEDGEMENTS

Based on Mojolicious::Plugin::Database because I don't want to leave the MongoDB crowd in the cold.

LICENSE AND COPYRIGHT

Copyright 2011 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.