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

Mandel::Collection - A collection of Mandel documents

SYNOPSIS

my $connection = MyModel->connect("mongodb://localhost/my_db");
my $persons = $connection->collection("person");

$persons->count(sub {
  my($persons, $err, $int) = @_;
});

# ...

DESCRIPTION

This class is used to describe a group of mongodb documents.

ATTRIBUTES

connection

An object that inherit from Mandel.

model

An object that inherit from Mandel::Model.

METHODS

all

$self = $self->all(sub { my($self, $err, $docs) = @_; });
$docs = $self->all;

Retrieves all documents from the database that match the given "search" query.

create

$document = $self->create;
$document = $self->create(\%args);

Returns a new object of a given type. This object is NOT inserted into the mongodb collection. You need to call "save" in Mandel::Document for that to happen.

%args is used to set the fields in the new document, NOT the attributes.

count

$self = $self->count(sub { my($self, $err, $int) = @_; });
$int  = $self->count;

Used to count how many documents the current "search" query match.

distinct

$self   = $self->distinct("field_name", sub { my($self, $err, $values) = @_; });
$values = $self->distinct("field_name");

Get all distinct values for key in this collection.

iterator

$iterator = $self->iterator;

Returns a Mandel::Iterator object based on the "search" performed.

patch

$self = $self->patch(\%changes, sub { my($self, $err, $doc) = @_ });
$self = $self->patch(\%changes);

This method can be used to add %changes to multiple documents in the collection. Which documents to update will be decided by the %query given to "search".

%extra arguments default to:

  • upsert: false

  • multi: true

remove

$self = $self->remove(sub { my($self, $err) = @_; });
$self = $self->remove;

Remove the documents that query given to "search".

save

$self = $self->save(\%document, sub { my($self, $err, $doc) = @_; );
$doc  = $self->save(\%document);

Used to save a document. The callback receives a Mandel::Document.

$clone = $self->search(\%query, \%extra);

Return a clone of the given collection, but with different %search and %extra parameters. You can chain these calls to make the query more precise.

%extra will be used to set extra parameters on the Mango::Cursor, where all the keys need to match the "ATTRIBUTES" in Mango::Cursor.

single

$self = $self->single(sub { my($self, $err, $doc) = @_; });
$doc  = $self->single;

Will return the first object found in the callback, matching the given %search query.

SEE ALSO

Mojolicious, Mango, Mandel

AUTHOR

Jan Henning Thorsen - jhthorsen@cpan.org