Friday, August 2, 2019

tailing mongo oplog with Perl

I haven't used Perl for over a decade, and I recently got curious to see  how Perl is with respect to the popular languages like Go and Python. Turns out Perl is far from stagnant with nice stuff such as carton package management.

As a practice I decided to try tailing the mongo oplog with Perl. I've written the code with Go and Python to deal with Mongo oplog  not long ago so it's still fresh. Turns out it's fairly easy to do for Perl


Fun!




use MongoDB;
print "hello ";
use feature ':5.10'; # for say
use Data::Dumper;
my $mongo_client = MongoDB::MongoClient->new(host=>'192.168.106.195',port=>7017);
=begin comment
obtaining oplog from "local" db
Every mongod instance has its own local database,
which stores data used in the replication process,
and other instance-specific data. The local database is invisible to replication: collections in the local database are not replicated.
=cut
my $db = $mongo_client->get_database('local');
my $ops_col = $db->get_collection('oplog.rs');
my $cursor = $ops_col->find({});
$cursor->tailable_await(1);
while(true){
say "polling\n";
while ( my $doc = $cursor->next ) {
say Dumper($doc);
}
sleep(2);
}