The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Mongol::Roles::Core - Core MongoDB actions and configuration

SYNOPSIS

package Models::Person {
	use Moose;

	extends 'Mongol::Model';

	with 'Mongol::Roles::Core';

	has 'first_name' => (
		is => 'ro',
		isa => 'Str',
		required => 1,
	);

	has 'last_name' => (
		is => 'ro',
		isa => 'Str',
		required => 1,
	);

	has 'age' => (
		is => 'rw',
		isa => 'Int',
		default => 0,
	);

	__PACKAGE__->meta()->make_immutable();
}

...

my $person = Models::Person->new(
	{
		first_name => 'Steve',
		last_name => 'Rogers',
	}
);

$person->save();
printf( "User id: %s\n", $person->id()->to_string() )

$person->age( 70 );
$person->save();

DESCRIPTION

Mongol core functionality, this takes care of all the basic actions. This role should be applied to master models.

ATTRIBUTES

collection

my $collection = Models::Person->collection();

my $collection = MongoDB->connect(...)
	->get_namespace( 'db.collection' );

Models::Person->collection( $collection );

id

my $id = $object->id();
$object->id( $id );

METHODS

find

my $cursor = Models::Person->find( $query, $options );

find_one

my $object = Models::Person->find_one( $query, $options );

retrieve

my $object = Models::Person->retrieve( $id );

Using the provided id values searches for the document in the collection and returns an instance of this model if found or undef otherwise.

count

my $count = Models::Person->count( $query, $options );

exists

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

Checks weather the document with id exists in the collection. Returns a boolean value indicating if the document exists or not.

update

my $count = Models::Person->update( $query, $update, $options );

delete

my $count = Models::Person->delete( $query );

Removes documents that match the $query form the associated collection. Returns the number of the documents removed or undef.

save

$object->save();

Inserts or updates the instance model.

remove

$object->remove();

Deletes the current object from the collection using the id property.

drop

Models::Person->drop();

Drops the MongoDB collection for this model.

to_object

my $object = Models::Person->to_object( $hashref );

Creates a model instance from a hashref document.

SEE ALSO