# Catalyst and MongoDB
## Catalyst::Model::MongoDB
Makes a MongoDB Connection available as a Catalyst Model, including several shortcuts for default collections and the like. [Getty](http://search.cpan.org/~getty/)'s [Catalyst::Model::MongoDB is on CPAN](http://search.cpan.org/perldoc?Catalyst::Helper::Model::MongoDB) and very useful
## Accessing MongoDB::ObjectID from within TT templates
TT2 disallows access to hash elements with a key that starts with an underscore by default and for good reasons. The MongoDB::ObjectID embedded in Mongo docs **do** start with an underscore however, to the `_id` attribute is inaccessible.
To get around this, one must write a method in the TT View and expose this to the template:
<pre lang="Perl">
package MyApp::View::HTML;
# ...
sub mongo_oid {
my ($self, $c, $mongo_doc) = @_;
return unless ref $mongo_doc eq 'HASH';
if ( $mongo_doc->{_id} && $mongo_doc->{value} ) {
return $mongo_doc->{_id}->{value};
}
return;
}
__PACKAGE__->config(
expose_methods => [qw/mongo_oid/],
);
</pre>
Now, in the template, the object ID of a mongo doc `mongodoc` is accessible as `[% mongo_oid( mongodoc ) %]`.