Mango::Database(3pm)
π Mango::Database π
okay bestie so this is Mango::Database, the MongoDB database situation, straight out of the User Contributed Perl Documentation (3pm) vibes β¨
πΈ NAME πΈ
Mango::Database β it's a MongoDB database, fr fr π
π SYNOPSIS π
like, here's the whole flex in four lines:
use Mango::Database;
my $db = Mango::Database->new(mango => $mango);
my $collection = $db->collection('foo');
my $gridfs = $db->gridfs;
no cap, that's literally it π
π¦ DESCRIPTION π¦
Mango::Database is a container for MongoDB databases used by Mango. it holds the vibes, it holds the collections, it holds the gridfs. main character energy only π
β¨ ATTRIBUTES β¨
Mango::Database implements the following attributes, and bestie you're gonna want to know them.
π mango
my $mango = $db->mango;
$db = $db->mango(Mango->new);
this is the Mango object that this database belongs to β its ride or die, its origin story π«Ά
β οΈ heads up though: this reference is usually weakened, so the Mango object needs to be referenced somewhere else too, or it'll ghost you. keep a strong ref, bestie, i'm serious π
πΈ name
my $name = $db->name;
$db = $db->name('bar');
the name of this database. that's it. that's the attribute. iconic β¨
π METHODS π
Mango::Database inherits all methods from Mojo::Base and implements the following new ones. so much talent in one class, honestly.
π build_write_concern
my $concern = $db->build_write_concern;
builds the write concern based on the mango settings (that's the one from L</"mango"> up there, iykyk). it's giving prepared π
π§ collection
my $collection = $db->collection('foo');
builds a Mango::Collection object for the collection. boom, instant collection π«
π collection_names
my $names = $db->collection_names;
gives you the names of all the collections in this database. you can filter the results by using the same arguments as for "list_collections" β yeah, the same ones, go look π β and you can also append a callback to do the operation non-blocking:
$db->collection_names(sub {
my ($db, $err, $names) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
don't forget the Mojo::IOLoop->start line or nothing's gonna happen and you'll be staring at your terminal like π
π command
my $doc = $db->command(bson_doc(text => 'foo.bar', search => 'test'));
my $doc = $db->command(bson_doc(getLastError => 1, w => 2));
my $doc = $db->command('getLastError', w => 2);
runs a command against the database. three whole flavors, pick your fave π¦
you can also append a callback to run the command non-blocking:
$db->command(('getLastError', w => 2) => sub {
my ($db, $err, $doc) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
same deal, Mojo::IOLoop->start is mandatory, periodt π
π¦ dereference
my $doc = $db->dereference($dbref);
resolves a database reference. mystery solved, detective era π you can also append a callback to do the operation non-blocking:
$db->dereference($dbref => sub {
my ($db, $err, $doc) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
β gridfs
my $gridfs = $db->gridfs;
builds a Mango::GridFS object. your files have never been so glamorous π
π list_collections
okay this one has receipts, look at all these options:
# return a cursor for all collections
my $cursor = $db->list_collections;
# only collections which name matchs a regex
my $cursor = $db->list_collections(filter => { name => qr{^prefix} });
# only capped collections
my $cursor = $db->list_collections(filter => { 'options.capped' => 1 });
# only the first 10 collections
my $cursor = $db->list_collections(cursor => { batchSize => 10 });
returns a Mango::Cursor of all the collections in this database. each collection is represented by a document containing at least the keys "name" and "options". you can also append a callback to do the operation non-blocking:
$db->list_collections(sub {
my ($db, $err, $cursor) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
(yes, Mojo::IOLoop->start again. i'm not nagging, i'm informing π«Ά)
π stats
my $stats = $db->stats;
gets the database statistics. numbers!! data!! obsessed πβ¨ you can also append a callback to do the operation non-blocking:
$db->stats(sub {
my ($db, $err, $stats) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
π₯ SEE ALSO π₯
- Mango
- Mojolicious::Guides
- <http://mojolicio.us>
and that's the whole page bestie, hope it slayed π
this masterpiece is documented as perl v5.30.3, dated 2020-06-05, and lives under the name Mango::Database(3pm) β go forth and query, queen πβ¨
π¦ where this page comes from
- debian bookworm: libmango-perl 1.30-2 Β· Artistic-2.0
- debian trixie: libmango-perl 1.30-2 Β· Artistic-2.0
- ubuntu jammy: libmango-perl 1.30-2 Β· Artistic-2.0
- ubuntu noble: libmango-perl 1.30-2 Β· Artistic-2.0
- ubuntu resolute: libmango-perl 1.30-2 Β· Artistic-2.0
- ubuntu stonking: libmango-perl 1.30-2 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms