Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoCursor hasNext iteration problem if nest find ,cursor will not iteration #91

Closed
shanghaiyangming opened this issue Apr 6, 2016 · 2 comments
Labels
Milestone

Comments

@shanghaiyangming
Copy link

<?php
include "vendor/autoload.php";

$client = new MongoClient('mongodb://127.0.0.1:27017');
$test1 = $client->selectCollection('test', 'test1');
$test2 = $client->selectCollection('test', 'test2');
$test1->drop();
$test2->drop();

for ($i = 0; $i < 5; $i ++) {
    $inertData = array(
        'a' => $i,
        'b' => time()
    );
    var_dump($test1->insert($inertData));
    $inertData1 = array(
        'a' => $i,
        'b' => time()
    );
    var_dump($test2->insert($inertData1));
}

$cursor = $test1->find([],['a'=>true]);
$cursor->limit(10);
$cursor->sort(array(
    '_id' => - 1
));

$data = array();
while ($cursor->hasNext()) {
    $row = $cursor->getNext();
    $test2->count(array(
        'a' => $row['a']
    ));
    $data[] = $row;
}

var_dump($data);

**# the correct result is a from 0 to 4

but the real result is :
double 3 like this:**

array(5) {
  [0]=>
  array(2) {
    ["_id"]=>
    object(MongoId)#43 (1) {
      ["objectID":"MongoId":private]=>
      object(MongoDB\BSON\ObjectID)#36 (1) {
        ["oid"]=>
        string(24) "5704d1c991a65e2384002817"
      }
    }
    ["a"]=>
    int(3)
  }
  [1]=>
  array(2) {
    ["_id"]=>
    object(MongoId)#48 (1) {
      ["objectID":"MongoId":private]=>
      object(MongoDB\BSON\ObjectID)#47 (1) {
        ["oid"]=>
        string(24) "5704d1c991a65e2384002817"
      }
    }
    ["a"]=>
    int(3)
  }
  [2]=>
  array(2) {
    ["_id"]=>
    object(MongoId)#45 (1) {
      ["objectID":"MongoId":private]=>
      object(MongoDB\BSON\ObjectID)#44 (1) {
        ["oid"]=>
        string(24) "5704d1c991a65e2384002815"
      }
    }
    ["a"]=>
    int(2)
  }
  [3]=>
  array(2) {
    ["_id"]=>
    object(MongoId)#52 (1) {
      ["objectID":"MongoId":private]=>
      object(MongoDB\BSON\ObjectID)#51 (1) {
        ["oid"]=>
        string(24) "5704d1c991a65e2384002813"
      }
    }
    ["a"]=>
    int(1)
  }
  [4]=>
  array(2) {
    ["_id"]=>
    object(MongoId)#54 (1) {
      ["objectID":"MongoId":private]=>
      object(MongoDB\BSON\ObjectID)#53 (1) {
        ["oid"]=>
        string(24) "5704d1c991a65e2384002811"
      }
    }
    ["a"]=>
    int(0)
  }
}

@shanghaiyangming
Copy link
Author

All hasNext() getNext() will triggering this bug.

@alcaeus alcaeus added the bug label Apr 7, 2016
@alcaeus alcaeus added this to the 1.0.2 milestone Apr 7, 2016
@alcaeus
Copy link
Owner

alcaeus commented Apr 8, 2016

Thanks for the example! This will be fixed in 1.0.2.

@alcaeus alcaeus closed this as completed in 0b27c0e Apr 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants