Mango:...:Query(3pm)

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

✨ NAME ✨

Mango::Cursor::Query β€” a MongoDB query cursor, bestie πŸ’…

It's from the User Contributed Perl Documentation (that's the 3pm manual section, iykyk), and this is literally its whole page, rewritten with love 🫢


🌸 SYNOPSIS 🌸

use Mango::Cursor::Query;

my $cursor = Mango::Cursor::Query->new(collection => $collection);
my $docs   = $cursor->all;

okay so that's the whole vibe β€” make a cursor, grab all the docs. slay ✨


πŸ¦‹ DESCRIPTION πŸ¦‹

Mango::Cursor::Query is a container for MongoDB query cursors used by Mango::Collection. That's it, that's the job. It holds your cursor, it lets you poke at it, and Mango::Collection is the one who actually uses it. lowkey simple, highkey essential πŸ’–


πŸŽ€ ATTRIBUTES πŸŽ€

Mango::Cursor::Query inherits all attributes from Mango::Cursor and implements the following new ones. not some of them, bestie β€” all of them πŸ‘‘

πŸ’« await_data

my $await = $cursor->await_data;
$cursor   = $cursor->await_data(1);

Await data. ⏳ (yes that's the whole description, no I will not elaborate, the original didn't either πŸ™ˆ)

πŸ’¬ comment

my $comment = $cursor->comment;
$cursor     = $cursor->comment('Fun query!');

A comment to identify the query. label your queries like you label your playlists, fr fr 🎧

πŸ” fields

my $fields = $cursor->fields;
$cursor    = $cursor->fields({foo => 1});

Select fields from documents. cherry-pick only the keys you want, no cap πŸ“

πŸ“ hint

my $hint = $cursor->hint;
$cursor  = $cursor->hint({foo => 1});

Force a specific index to be used. it's giving "use THIS one, I said what I said" πŸ’…

πŸ›‘ max_scan

my $max = $cursor->max_scan;
$cursor = $cursor->max_scan(500);

Limit the number of documents to scan. βœ‹ don't go overboard

⏰ max_time_ms

my $max = $cursor->max_time_ms;
$cursor = $cursor->max_time_ms(500);

Timeout for the query, in milliseconds. the query has a curfew and it's 500ms, periodt βŒ›

πŸ“ query

my $query = $cursor->query;
$cursor   = $cursor->query({foo => 'bar'});

The original query. the receipts, the source material, the origin story πŸ“œ

πŸ—³οΈ read_preference

my $pref = $cursor->read_preference;
$cursor  = $cursor->read_preference({mode => 'SECONDARY'});

Read preference. who gets to serve the reads πŸ‘€

⏭️ skip

my $skip = $cursor->skip;
$cursor  = $cursor->skip(5);

Number of documents to skip β€” defaults to 0. no skipping by default, we're not toxic like that 🧚

πŸ“Έ snapshot

my $snapshot = $cursor->snapshot;
$cursor      = $cursor->snapshot(1);

Use snapshot mode. πŸ“· freeze the moment

πŸ”€ sort

my $sort = $cursor->sort;
$cursor  = $cursor->sort({foo => 1});
$cursor  = $cursor->sort(bson_doc(foo => 1, bar => -1));

Sort documents. ⚠️ the order of keys matters β€” do NOT come for me about it, it's just true, the ordering is part of the sort and that's that πŸ‘‘

🌊 tailable

my $tailable = $cursor->tailable;
$cursor      = $cursor->tailable(1);

Tailable cursor. it keeps the tail wagging πŸ•


πŸ‘‘ METHODS πŸ‘‘

Mango::Cursor::Query inherits all methods from Mango::Cursor and implements the following new ones. yes, all of them again. iconic behavior ✨

πŸ› οΈ build_query

my $query = $cursor->build_query;
my $query = $cursor->build_query($explain);

Generate the final query with the cursor attributes. this is where all your little settings finally become One Real Query 🧚

πŸ‘― clone

my $clone = $cursor->clone;

Clone the cursor. everybody needs a twin πŸ’–

πŸ”’ count

my $count = $cursor->count;

Count the number of documents this cursor can return. You can also append a callback to perform the operation non-blocking. look at her go, multitasking ✨

$cursor->count(sub {
  my ($cursor, $err, $count) = @_;
  ...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
note the Mojo::IOLoop->start unless Mojo::IOLoop->is_running; β€” you need it or nothing happens and you'll be sitting there like πŸ‘οΈπŸ‘„πŸ‘οΈ

🎨 distinct

my $values = $cursor->distinct('foo');

Get all the distinct values for a key. You can also append a callback to perform the operation non-blocking.

$cursor->distinct(foo => sub {
  my ($cursor, $err, $values) = @_;
  ...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

dedupe your life πŸ’œ

πŸ”¬ explain

my $doc = $cursor->explain;

Provide information on the query plan. You can also append a callback to perform the operation non-blocking.

$cursor->explain(sub {
  my ($cursor, $err, $doc) = @_;
  ...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

it's giving "explain yourself" and honestly? we love the transparency 🫢


🌈 SEE ALSO 🌈


perl v5.30.3 Β· 2020-06-05 Β· Mango:...:Query(3pm)

and that's the whole page bestie, mwah πŸ’‹ go forth and query responsibly πŸ”₯πŸ’–