MongoDBx::Class::Document - 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 ]); }; __PACKAGE__->meta->make_immutable;
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.
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:
$set
$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;
$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.
update()
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 dependant on this document (such as those that reference it with belongs_to) will not be removed too.
remove()
belongs_to
The following methods are only to be used internally.
Convenience method, shortcut for $doc->_collection->_database.
$doc->_collection->_database
Convenience method, shortcut for $doc->_database->_connection.
$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.
update( \%changes )
Ido Perlmuter, <ido at ido50.net>
<ido at ido50.net>
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.
bug-mongodbx-class at rt.cpan.org
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
http://rt.cpan.org/NoAuth/Bugs.html?Dist=MongoDBx::Class
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/MongoDBx::Class
CPAN Ratings
http://cpanratings.perl.org/d/MongoDBx::Class
Search CPAN
http://search.cpan.org/dist/MongoDBx::Class/
MongoDBx::Class::Moose, MongoDBx::Class::EmbeddedDocument.
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.
To install MongoDBx::Class, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MongoDBx::Class
CPAN shell
perl -MCPAN -e shell install MongoDBx::Class
For more information on module installation, please visit the detailed CPAN module installation guide.