NAME
Mongoose::Join - simple class relationship resolver
VERSION
version 0.13
SYNOPSIS
package Author;
use Moose;
with 'Mongoose::Document';
has 'articles' => (
is => 'rw',
isa => 'Mongoose::Join[Article]',
default => sub { Mongoose::Join->new( with_class => 'Article' ) }
);
DESCRIPTION
This module can be used to establish relationships between two Mongoose::Document
classes. It should not be used with Mongoose::EmbeddedDocument
classes.
All object relationships are stored as reference $id
arrays into the parent object. This translates into a slight performance hit when loading the parent class, but not as much as loading all objects at one as when using an ArrayRef
.
Attention: the relationship attribute needs to be initialized to an instance of Mongoose::Join
before it can be used.
Mongoose::Class
Take a look at Mongoose::Class, it has nice syntatic sugar that does most of the initialization behind the scenes for you.
METHODS
add
Add (join) a Mongoose::Document object for later saving.
Saving the parent Mongoose::Document will save both.
my $author = Author->new;
$author->articles->add( Article->new );
$author->save; # saves all objects
remove
Delete from the relationship list.
my $author = Author->find_one;
my $first_article = $author->articles->find_one;
$author->articles->remove( $first_article );
find
Run a MongoDB find
on the joint collection.
# searches for articles belonging to this collection
my $cursor = $author->articles->find({ title=>'foo article' });
while( my $article = $cursor->next ) {
...
}
Returns a Mongoose::Cursor.
find_one
Just like find, but with a find_one
twist.
all
Same as find
, but returns an ARRAY with all the results, instead of a cursor.
query
Run a MongoDB query
on the joint collection.
collection
Returns the MongoDB::Collection for the joint collection.
with_collection_name
Return the collection name for the joint Mongoose::Document.