Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Contact Us
Online Help
Domain Status
Man Pages

Virtual Servers

Topology Map

Server Agreement
Year 2038

USA Flag



Man Pages

Manual Reference Pages  -  MOJOLICIOUS::PLUGIN::MONGODB (3)

.ds Aq ’


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



version 1.16


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,


    helper          (optional)  The name to give to the easy-access helper if you want
                                to change its name from the default db

    no_db               (optional)  Dont install the db helper
    no_model            (optional)  Dont install the model helper
    no_coll             (optional)  Dont install the coll helper
    no_find_and_modify  (optional)  Dont install the find_and_modify helper
    no_map_reduce       (optional)  Dont install the map_reduce helper
    no_group            (optional)  Dont install the group helper

All other options passed to the plugin are used to connect to MongoDB.



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


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 });


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
        my $collection = $self->coll(foo);

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


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(>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 <> 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.


Ben van Staveren, <madcat at>


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


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

    perldoc Mojolicious::Plugin::Mongodb

You can also look for information at:
o AnnoCPAN: Annotated CPAN documentation


o CPAN Ratings


o Search CPAN



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


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 for more information.

Search for    or go to Top of page |  Section 3 |  Main Index

perl v5.20.3 MOJOLICIOUS::PLUGIN::MONGODB (3) 2014-05-21

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.