PHP driver - problem with batchSize and sorting

105 views
Skip to first unread message

Matt Parlane

unread,
Aug 14, 2012, 12:31:06 AM8/14/12
to mongo...@googlegroups.com
Hi all...

I added the following test:

diff --git a/tests/MongoCursorTest.php b/tests/MongoCursorTest.php
index 9519f55..ffd9aa5 100644
--- a/tests/MongoCursorTest.php
+++ b/tests/MongoCursorTest.php
@@ -786,6 +786,15 @@ class MongoCursorTest extends PHPUnit_Framework_TestCase
             }
         }
         $this->assertEquals(2000, $count);
+
+        $count = 0;
+        $cursor->reset();
+        $cursor->batchSize(20)->sort(array('y' => 1));
+        while ($cursor->hasNext()) {
+            $cursor->getNext();
+            $count++;
+        }
+        $this->assertEquals(2000, $count);
     }
 
     public function testExplainReset() {

And it fails:

2) MongoCursorTest::testBatchSize
Failed asserting that 20 matches expected 2000.

/home/wg/mongo-php-driver/tests/MongoCursorTest.php:797

From my reading of the docs, the test should pass -- am I right?

If I am, I can send a pull request with the new test. I'd much rather send a pull request with a fix as well, but I haven't been able to figure out what's going wrong from looking through the driver source.

Thanks,

Matt

Derick Rethans

unread,
Aug 14, 2012, 6:06:12 AM8/14/12
to mongo...@googlegroups.com
Hi Matt!

On Mon, 13 Aug 2012, Matt Parlane wrote:

> I added the following test:
>
> diff --git a/tests/MongoCursorTest.php b/tests/MongoCursorTest.php
> index 9519f55..ffd9aa5 100644
> --- a/tests/MongoCursorTest.php
> +++ b/tests/MongoCursorTest.php

Just FYI: We're migrating away from the phpunit tests and instead will
use the phpt tests (in the tests directory too)

> @@ -786,6 +786,15 @@ class MongoCursorTest extends
> PHPUnit_Framework_TestCase
> }
> }
> $this->assertEquals(2000, $count);
> +
> + $count = 0;
> + $cursor->reset();
> + $cursor->batchSize(20)->sort(array('y' => 1));
> + while ($cursor->hasNext()) {
> + $cursor->getNext();
> + $count++;
> + }
> + $this->assertEquals(2000, $count);
> }
>
> public function testExplainReset() {
>
> And it fails:
>
> 2) MongoCursorTest::testBatchSize
> Failed asserting that 20 matches expected 2000.
>
> /home/wg/mongo-php-driver/tests/MongoCursorTest.php:797
>
> From my reading of the docs, the test should pass -- am I right?

Maybe, it might have to do with the $cursor->reset() or something. Could
you please file a bugreport at https://jira.mongodb.org/browse/PHP so
that we can track this? We'll take it from there then. It would be best
if it contained a self-contained script (just a .php) file instead of a
patch to a phpunit test.

cheers,
Derick

--
http://mongodb.org | http://derickrethans.nl
twitter: @derickr and @mongodb

Derick Rethans

unread,
Sep 25, 2012, 11:48:51 AM9/25/12
to mongo...@googlegroups.com
On Tue, 25 Sep 2012, Alex Montgomery wrote:

> Hello all,
>
> I am seeing a similar issue. The only difference between my code and what
> Matt has written is the order in which I set the batchSize and sort on the
> cursor.
>
> I do something like:
> $cursor = $mongo->{$table_name}->find($mongo_args)->sort(array('ts' => 1));
> $cursor->batchSize(5000);

Order doesn't matter, as the query only gets send to the server when you
start iterating with foreach().

> Would this cause any discrepancies. Derick, I read your article on
> how Mongo cursors work in PHP ... really great stuff. It is this
> article that inspired me to use the batchSize because the logs I am
> pulling from mongo are enormous. Any idea on when this issue might be
> resolved?

The thread initiator didn't give us an issue - if you can create one
with a full dataset and example for us to try to reproduce it, we'd be
happy to look at it.

cheers,
Derick

--
{
website: [ "http://mongodb.org", "http://derickrethans.nl" ],
twitter: [ "@derickr", "@mongodb" ]
}
Reply all
Reply to author
Forward
0 new messages