Skip to content
genio edited this page Sep 10, 2014 · 10 revisions

Using MongoDB in Mojolicious

Make a helper in startup to access the schema so you can do something like $self->db from your controller

$self->attr(db => sub { 
    MongoDB::Connection
        ->new(host => $config->{database_host})
        ->get_database($config->{database_name});
});
$self->helper('db' => sub { shift->app->db });

The reason for the attr approach is so that each child will init its own MongoDB connection which is required by the MongoDB driver

Mongo alternative for Mojolicious

Of course, as of February 2013, you could use the Mango experimental Mojolicious spin-off driver by Sebastian Riedel.

It's Mangolicious - or, very much in sync with Mojolicious logic and implementation. While being experimental, it specifically makes sure not to lose any of your data and to be fully asynchronous, both of which is not exactly true with the MongoDB driver.

Clone this wiki locally