Mongol::Roles::Core - Core MongoDB actions and configuration
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();
Mongol core functionality, this takes care of all the basic actions. This role should be applied to master models.
my $collection = Models::Person->collection(); my $collection = MongoDB->connect(...) ->get_namespace( 'db.collection' ); Models::Person->collection( $collection );
my $id = $object->id(); $object->id( $id );
my $cursor = Models::Person->find( $query, $options );
my $object = Models::Person->find_one( $query, $options );
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.
id
undef
my $count = Models::Person->count( $query, $options );
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.
my $count = Models::Person->update( $query, $update, $options );
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.
$query
$object->save();
Inserts or updates the instance model.
$object->remove();
Deletes the current object from the collection using the id property.
Models::Person->drop();
Drops the MongoDB collection for this model.
my $object = Models::Person->to_object( $hashref );
Creates a model instance from a hashref document.
MongoDB::Collection
To install Mongol, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mongol
CPAN shell
perl -MCPAN -e shell install Mongol
For more information on module installation, please visit the detailed CPAN module installation guide.