NAME
Mango::Collection - MongoDB collection
SYNOPSIS
use Mango::Collection;
my $collection = Mango::Collection->new(db => $db);
my $cursor = $collection->find({foo => 'bar'});
DESCRIPTION
Mango::Collection is a container for MongoDB collections used by Mango::Database.
ATTRIBUTES
Mango::Collection implements the following attributes.
db
my $db = $collection->db;
$collection = $collection->db(Mango::Database->new);
Mango::Database object this collection belongs to.
name
my $name = $collection->name;
$collection = $collection->name('bar');
Name of this collection.
METHODS
Mango::Collection inherits all methods from Mojo::Base and implements the following new ones.
find
my $cursor = $collection->find({foo => 'bar'});
Get Mango::Cursor object for query.
find_one
my $doc = $collection->find_one({foo => 'bar'});
my $doc = $collection->find_one($oid);
Find one document. You can also append a callback to perform operation non-blocking.
$collection->find_one({foo => 'bar'} => sub {
my ($collection, $err, $doc) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
full_name
my $name = $collection->full_name;
Full name of this collection.
insert
my $oid = $collection->insert({foo => 'bar'});
my $oids = $collection->insert([{foo => 'bar'}, {baz => 'yada'}]);
Insert one or more documents into collection. You can also append a callback to perform operation non-blocking.
$collection->insert({foo => 'bar'} => sub {
my ($collection, $err, $oid) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
remove
my $doc = $collection->remove;
my $doc = $collection->remove({foo => 'bar'});
my $doc = $collection->remove({foo => 'bar'}, {single => 1});
Remove documents from collection. You can also append a callback to perform operation non-blocking.
$collection->remove(({foo => 'bar'}, {single => 1}) => sub {
my ($collection, $err, $doc) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
These options are currently available:
- single
-
Remove only one document.
update
my $doc = $collection->update({foo => 'bar'}, {foo => 'baz'});
my $doc = $collection->update({foo => 'bar'}, {foo => 'baz'}, {multi => 1});
Update document in collection. You can also append a callback to perform operation non-blocking.
$collection->update(({foo => 'bar'}, {foo => 'baz'}, {multi => 1}) => sub {
my ($collection, $err, $doc) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
These options are currently available:
- multi
-
Update more than one document.
- upsert
-
Insert document if none could be updated.