NAME
MongoDBx::Class::Document - A MongoDBx::Class document role
VERSION
version 1.030002
SYNOPSIS
# 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 ]);
};
__PACKAGE__->meta->make_immutable;
DESCRIPTION
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.
ATTRIBUTES
The following attributes are provided:
_id
The document's internal ID, represented as a MongoDB::OID object. This is a required attribute.
_collection
The MongoDBx::Class::Collection object representing the MongoDB collection in which the document is stored. This is a required attribute.
_class
A string. The name of the document class of this document. This is a required attribute.
OBJECT METHODS
The following object methods are provided:
id()
oid()
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 dependant 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');
$doc->set_year(1895);
$doc->update;
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.
delete()
remove()
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 dependant on this document (such as those that reference it with belongs_to
) will not be removed too.
INTERNAL METHODS
The following methods are only to be used internally.
_database()
Convenience method, shortcut for $doc->_collection->_database
.
_connection()
Convenience method, shortcut for $doc->_database->_connection
.
_attributes()
Returns a list of names of all attributes the document object has, minus '_collection' and '_class', sorted alphabetically.
_update_self()
Used by the update( \%changes )
method to update the object instance with the new values.
AUTHOR
Ido Perlmuter, <ido at ido50.net>
BUGS
Please report any bugs or feature requests to bug-mongodbx-class at rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MongoDBx-Class. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc MongoDBx::Class::Document
You can also look for information at:
RT: CPAN's request tracker
AnnoCPAN: Annotated CPAN documentation
CPAN Ratings
Search CPAN
SEE ALSO
MongoDBx::Class::Moose, MongoDBx::Class::EmbeddedDocument.
LICENSE AND COPYRIGHT
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 http://dev.perl.org/licenses/ for more information.