NAME
Mongol::Entity
SYNOPSIS
package Address {
use Moose;
extends 'Mongol::Base';
has 'street' => (
is => 'ro',
isa => 'Str',
required => 1
);
has 'number' => (
is => 'ro',
isa => 'Int',
required => 1,
);
__PACKAGE__->meta()->make_immutable();
}
package Person {
use Moose;
extends 'Mongol::Base';
with 'Mongol::Entity';
has 'first_name' => (
is => 'ro',
isa => 'Str',
required => 1,
);
has 'last_name' => (
is => 'ro',
isa => 'Str',
required => 1,
);
has 'age' => (
is => 'ro',
isa => 'Int',
required => 1,
);
has 'addresses' => (
is => 'ro',
isa => 'ArrayRef[Address]',
default => sub { [] },
traits => [ qw( Array ) ],
handles => {
add_address => 'push',
}
);
__PACKAGE__->meta()->make_immutable();
}
...
my $person = Person->new(
{
id => 6161742,
first_name => 'John',
last_name => 'Doe',
age => 30,
}
);
my $home = Address->new(
{
street => 'Main St.',
number => 123
}
);
# --- Saving
$person->add_address( $home );
$person->save();
$person->age( 31 );
$person->save();
# --- Reading
my $other = Person->retrive( 616742 );
DESCRIPTION
This is the heart and sould of Mongol, when applied to a model will add all the CRUD functionlity for MongoDB.
ATTRIBUTES
collection
my $mongo = MongoDB->connect();
my $collection = $mongo->get_namespace( 'db.collection' );
Person->collection( $collection );
MongoDB collection class attribute. It contains the MongoDB::Collection associated with this entity.
id
my $id = $model->id();
my $current_id = $model->id( '12345' );
METHODS
find
my $cursor = Person->find( { age => 30 }, {} );
Executes the mongo query returning a Mongol::Cursor object. Supports the same parameters as the find method definded in the MongoDB::Collection package.
find_one
my $model = Person->find( { name => 'John Doe' }, {} );
Retrieves a single entity from the collection. For more details see find_one on MongoDB::Collection. Returns undef if the value was not found.
retrieve
my $model = Person->retrieve( $id );
Retrieves a single entity based on id. Returns undef if the record was not found.
count
my $count = Person->count( { age => '30' }, {} );
Counts the objects in collection given the current query.
exists
my $bool = Person->exists( $id );
paginate
my $collection = Person->paginate( { age => { '$lt' => 40 } }, {}, 30, 10 );
update
delete
my $count = Person->delete( { age => { '$gt' => 30 } } );
save
my $person = Person->new( { name => 'John Doe', age => 34 } );
$person->save();
$person->age( 35 );
$person->save();
remove
$model->remove();
drop
Person->drop();
Drops the MongoDB collection associated to this entity.