Skip to content

Instantly share code, notes, and snippets.

@dagolden
Created March 18, 2015 19:02
Show Gist options
  • Save dagolden/a4a194b5ed88f6fd0e6a to your computer and use it in GitHub Desktop.
Save dagolden/a4a194b5ed88f6fd0e6a to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use v5.10;
use strict;
use warnings;
use MongoDB;
use JSON::MaybeXS;
use boolean;
my $json = JSON->new->convert_blessed->canonical;
my $mc = MongoDB::MongoClient->new( host => $ENV{MONGOD} // 'localhost' );
my $db = $mc->get_database("test");
my $coll = $db->get_collection("fam");
my $build =
$mc->get_database('admin')->get_collection('$cmd')->find_one( { buildInfo => 1 } );
my ($version_str) = $build->{version} =~ m{^([0-9.]+)};
my @cases = (
{
label => "find_one_and_replace upsert: new false, no sort",
params => [
query => { _id => 1 },
update => { y => 1 },
upsert => true,
new => false,
],
},
{
label => "find_one_and_replace upsert: new true, no sort",
params => [
query => { _id => 1 },
update => { y => 1 },
upsert => true,
new => true,
],
},
{
label => "find_one_and_replace upsert: new false, with sort",
params => [
query => { _id => 1 },
update => { y => 1 },
upsert => true,
new => false,
sort => { x => 1 },
],
},
{
label => "find_one_and_replace upsert: new true, with sort",
params => [
query => { _id => 1 },
update => { y => 1 },
upsert => true,
new => true,
sort => { _id => 1 },
],
},
{
label => "find_one_and_update upsert: new false, no sort",
params => [
query => { _id => 1 },
update => { '$set' => { y => 1} },
upsert => true,
new => false,
],
},
{
label => "find_one_and_update upsert: new true, no sort",
params => [
query => { _id => 1 },
update => { '$set' => { y => 1} },
upsert => true,
new => true,
],
},
{
label => "find_one_and_update upsert: new false, with sort",
params => [
query => { _id => 1 },
update => { '$set' => { y => 1} },
upsert => true,
new => false,
sort => { x => 1 },
],
},
{
label => "find_one_and_update upsert: new true, with sort",
params => [
query => { _id => 1 },
update => { '$set' => { y => 1} },
upsert => true,
new => true,
sort => { _id => 1 },
],
},
);
say "MongoDB $version_str";
say "-" x 50;
for my $c (@cases) {
$coll->drop;
my $cmd = [ findAndModify => 'fam', @{ $c->{params} } ];
say $c->{label};
my $res = $db->run_command($cmd);
my $copy = { value => $res->{value} };
say $json->encode($copy);
say "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment