MongoDBx::Class::Document(3) A MongoDBx::Class document role


version 1.030002


# create a document class
package MyApp::Schema::Novel;
use MongoDBx::Class::Moose; # use this instead of Moose;
use namespace::autoclean;
with 'MongoDBx::Class::Document';
has 'title' => (is => 'ro', isa => 'Str', required => 1, writer => 'set_title');
holds_one 'author' => (is => 'ro', isa => 'MyApp::Schema::PersonName', required => 1, writer => 'set_author');
has 'year' => (is => 'ro', isa => 'Int', predicate => 'has_year', writer => 'set_year');
has 'added' => (is => 'ro', isa => 'DateTime', traits => ['Parsed'], required => 1);
has 'review_count' => (is => 'rw', isa => 'Int', traits => ['Transient'], builder => '_build_review_count');

holds_many 'tags' => (is => 'ro', isa => 'MyApp::Schema::Tag', predicate => 'has_tags');
joins_one 'synopsis' => (is => 'ro', isa => 'Synopsis', coll => 'synopsis', ref => 'novel');
has_many 'related_novels' => (is => 'ro', isa => 'Novel', predicate => 'has_related_novels', writer => 'set_related_novels', clearer => 'clear_related_novels');
joins_many 'reviews' => (is => 'ro', isa => 'Review', coll => 'reviews', ref => 'novel');
sub _build_review_count { shift->reviews->count }
sub print_related_novels {
my $self = shift;
foreach my $other_novel ($self->related_novels) {
print $other_novel->title, ', ',
$other_novel->year, ', ',
$other_novel->author->name, "\n";
around 'reviews' => sub {
my ($orig, $self) = (shift, shift);
my $cursor = $self->$orig;

return $cursor->sort([ year => -1, title => 1, 'author.last_name' => 1 ]);


MongoDBx::Class::Document is a Moose role meant to be consumed by document classes. It provides expanded MongoDB documents with some common attributes, and needed methods for easy updating and deleting of documents.


The following attributes are provided:


The document's internal ID, represented as a MongoDB::OID object. This is a required attribute.


The MongoDBx::Class::Collection object representing the MongoDB collection in which the document is stored. This is a required attribute.


A string. The name of the document class of this document. This is a required attribute.


The following object methods are provided:



Both methods are equivalent. They are convenience methods that return the documents internal MongoDB OID in string format.

update( [ \%object, [ \%options ] ] )

Saves a new version of the document to the database. The behavior of this method is dependent on the existance or non-existance of an object hash-ref:

If an object hash-ref is provided, all of its key-value pairs are collapsed, and a $set update is performed on them. For example:

        $doc->update({ author => 'Sir Arthur Conan Doyle', year => 1895 }, $options)

Will effectively result in something like this being performed:

        $coll->update({ _id => $doc->_id }, { '$set' => { author => 'Sir Arthur Conan Doyle', year => 1895 } }, $options)

If an object hash-ref isn't provided, the entire document object is collapsed and an aggresive update is performed (i.e. an entirely new version of the document, representing the current state of the document's attributes, is saved to the database. For example:

        my $doc = find_one($id);
        $doc->set_author('Sir Arthur Conan Doyle');

Will effectively result in something like this being performed:

        $coll->update({ _id => $doc->_id }, $collapsed_doc, $options)

You can pass an options hash-ref just like with the "update()" method of MongoDBx::Class::Collection, but only if you pass an update object also.

Returns the output of MongoDB::Collection's original "update()" method.



Both methods are equivalent. They are shortcut methods for invoking the collection's "remove()" method on this document only. So, umm, they remove the document. But note that this operation does not cascade, so documents which are considered dependent on this document (such as those that reference it with "belongs_to") will not be removed too.


The following methods are only to be used internally.


Convenience method, shortcut for "$doc->_collection->_database".


Convenience method, shortcut for "$doc->_database->_connection".


Returns a list of names of all attributes the document object has, minus '_collection' and '_class', sorted alphabetically.


Used by the "update( \%changes )" method to update the object instance with the new values.


Ido Perlmuter, "<ido at>"


Please report any bugs or feature requests to "bug-mongodbx-class at", or through the web interface at <>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


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

        perldoc MongoDBx::Class::Document

You can also look for information at:


Copyright 2010-2014 Ido Perlmuter.

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.