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

EVENTS

None at this moment.

ATTRIBUTES

collection

        Person->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' }, {} );

retrieve

        my $model = Person->retrieve( $id );

count

        my $count = Person->count( { age => '30' }, {} );

exists

        my $bool = Person->exists( $id );

update

delete

        my $count = Person->delete( { age => { '$gt' => 30 } } );

save

        $model->age( 35 );
        $model->save();

remove

        $model->remove();

drop

        Person->drop();

Drops the MongoDB collection associated to this entity.

SEE ALSO