From 3d0b8ce46578fa3faa610ef6d287a1e9fdafde77 Mon Sep 17 00:00:00 2001 From: Robert Sedlacek Date: Tue, 1 May 2018 22:10:49 +0200 Subject: [PATCH 1/4] Test and fix for 'num' field of cursor info not being increased by the number of returned results --- lib/MongoDB/QueryResult.pm | 2 +- t/cursor.t | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/MongoDB/QueryResult.pm b/lib/MongoDB/QueryResult.pm index 3de04796..fa4dc8fc 100644 --- a/lib/MongoDB/QueryResult.pm +++ b/lib/MongoDB/QueryResult.pm @@ -138,7 +138,7 @@ has _cursor_num => ( isa => Num, ); -sub _inc_cursor_num { $_[0]{_cursor_num}++ } +sub _inc_cursor_num { $_[0]{_cursor_num} += $_[1] } has _docs => ( is => 'ro', diff --git a/t/cursor.t b/t/cursor.t index 3ce948ac..eaff581f 100644 --- a/t/cursor.t +++ b/t/cursor.t @@ -305,6 +305,7 @@ $coll->drop; $cursor->all; $info = $cursor->info; is($info->{at}, 1000); + is($info->{num}, 1000, 'cursor_num after ->all'); } # sort_by From 9a335ab0a4e0ca8302f1f2ce6f97713ca89df679 Mon Sep 17 00:00:00 2001 From: Robert Sedlacek Date: Tue, 1 May 2018 22:24:10 +0200 Subject: [PATCH 2/4] Test and fix for query limit propagating into QueryResult --- lib/MongoDB/Role/_CommandCursorOp.pm | 7 ++++++- t/cursor.t | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/MongoDB/Role/_CommandCursorOp.pm b/lib/MongoDB/Role/_CommandCursorOp.pm index 5fc13d81..aefa3d20 100644 --- a/lib/MongoDB/Role/_CommandCursorOp.pm +++ b/lib/MongoDB/Role/_CommandCursorOp.pm @@ -50,6 +50,11 @@ sub _build_result_from_cursor { $max_time_ms = $self->maxAwaitTimeMS if $self->maxAwaitTimeMS; } + my $limit = 0; + if ($self->isa('MongoDB::Op::_Query')) { + $limit = $self->limit if $self->limit; + } + my $batch = $c->{firstBatch}; my $qr = MongoDB::QueryResult->_new( _client => $self->client, @@ -58,7 +63,7 @@ sub _build_result_from_cursor { _bson_codec => $self->bson_codec, _batch_size => scalar @$batch, _cursor_at => 0, - _limit => 0, + _limit => $limit, _cursor_id => $c->{id}, _cursor_start => 0, _cursor_flags => {}, diff --git a/t/cursor.t b/t/cursor.t index eaff581f..c58138ff 100644 --- a/t/cursor.t +++ b/t/cursor.t @@ -89,6 +89,12 @@ my @values; is ($values[2]->{foo}, 4); } +# limit propagation to result +{ + my $c = $coll->query({}, { limit => 3, sort_by => { foo => 1 } }); + is($c->result->_limit, 3, 'query limit was propagated'); +} + # skip { @values = $coll->query({}, { limit => 3, skip => 1, sort_by => { foo => 1 } })->all; From 2002a614797d3905bc06e3d19315bd2265ee1091 Mon Sep 17 00:00:00 2001 From: Robert Sedlacek Date: Tue, 1 May 2018 22:54:09 +0200 Subject: [PATCH 3/4] Test and fix for bulk_write not passing along the writeConcern option --- lib/MongoDB/Collection.pm | 5 ++++- t/collection.t | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/MongoDB/Collection.pm b/lib/MongoDB/Collection.pm index fcd8e630..df3f3062 100644 --- a/lib/MongoDB/Collection.pm +++ b/lib/MongoDB/Collection.pm @@ -1668,7 +1668,10 @@ sub bulk_write { } } - return $bulk->execute( undef, { session => $session } ); + return $bulk->execute( + $options->{writeConcern}, + { session => $session }, + ); } BEGIN { diff --git a/t/collection.t b/t/collection.t index 6ecae76c..1fe139c9 100644 --- a/t/collection.t +++ b/t/collection.t @@ -945,6 +945,17 @@ subtest "querying w/ collation" => sub { } }; +subtest "bulk_write writeConcern used" => sub { + $coll->drop; + + like exception { + $coll->bulk_write( + [insert_one => [{ x => 3 }]], + { writeConcern => { w => 999 } }, + ); + }, qr/WriteConcernError/, 'write concern is taken into account'; +}; + my $js_str = 'function() { return this.a > this.b }'; my $js_obj = MongoDB::Code->new( code => $js_str ); From bf8a8985f67cde0fe9d2a667f4dbe64f1463bebf Mon Sep 17 00:00:00 2001 From: Robert Sedlacek Date: Wed, 2 May 2018 00:00:30 +0200 Subject: [PATCH 4/4] Guard bulk_write writeConcern test to only run on RSPrimary, due to {w:999} requirements --- t/collection.t | 3 +++ 1 file changed, 3 insertions(+) diff --git a/t/collection.t b/t/collection.t index 1fe139c9..0ff490a9 100644 --- a/t/collection.t +++ b/t/collection.t @@ -946,6 +946,9 @@ subtest "querying w/ collation" => sub { }; subtest "bulk_write writeConcern used" => sub { + plan skip_all => "Test requires ReplicaSet" + unless $server_type eq 'RSPrimary'; + $coll->drop; like exception {