Skip to content

Instantly share code, notes, and snippets.

@ian-kent
Last active December 29, 2015 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ian-kent/7676493 to your computer and use it in GitHub Desktop.
Save ian-kent/7676493 to your computer and use it in GitHub Desktop.
Mango cursor repeating in non-blocking mode
use Mango;
use strict;
use warnings;
print "Mango version: $Mango::VERSION\n";
my $mango = Mango->new($ENV{MANGO_URI} // "mongodb://localhost:27017");
my $db = $mango->db('test_db');
my $collection = $db->collection('test_collection');
$collection->remove();
$collection->insert([
{ n => 1 },
{ n => 2 },
{ n => 3 },
]);
my $c = $collection->find({ n => { '$gte' => 0 } });
while(my $doc = $c->next) {
print $doc->{n};
}
print "\n";
my $c2 = $collection->find({ n => { '$gte' => 0 } });
my $f = 0;
$c2->next(sub {
my ($collection, $error, $doc) = @_;
print $doc->{n};
Mojo::IOLoop->stop if ++$f == 6;
}) for (1..6);
Mojo::IOLoop->start;
print "\n";
Mango version: 0.19
123
123123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment