Mango::Database(3pm)

πŸ“¦
version 1.30 Β· 3 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– 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 πŸ”₯


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 πŸ‘‘βœ¨