0

I have built an application using MongoDB 3.2. I am trying to migrate it to MongoDB 3.4 as it includes several much needed features. Most of the migration went smoothly except for one thing - aggregate functions. My current aggregate functions look like this:

$collection = $mongo->getCollection('users');
$results = $collection->aggregate(array(
    array('$match' => array('_id' => $_SESSION['user'])),
    array('$project' => array('viewed_cases' => 1)),
    array('$unwind' => '$viewed_cases'),
));

However, this generates these issues in mongod:

Use of the aggregate command without the 'cursor' option is deprecated. See http://dochub.mongodb.org/core/aggregate-without-cursor-deprecation.

After reading the documentation, I have tried changing the function to this, adding cursor with batchSize set to the default (trying to add just cursor with empty array, as suggested in the documentation, has caused PHP to fail):

$collection = $mongo->getCollection('users');
$results = $collection->aggregate(array(
    array('$match' => array('_id' => $_SESSION['user'])),
    array('$project' => array('viewed_cases' => 1)),
    array('$unwind' => '$viewed_cases'),
), array('cursor' => array('batchSize' => 101)));

However, this changes the response sent to $results. Instead of the array looking like this:

array(2) {
  ["result"]=>
  array(4) {
    [0]=>
    array(2) {
      ["_id"]=>
      string(4) "idan"
      ["viewed_cases"]=>
      array(2) { ... }
    }
  }
}

which I've accessed through $results['result'], now I am getting this:

array(2) {
  ["cursor"]=>
  array(3) {
    ["id"]=>
    object(MongoInt64)#10 (1) {
      ["value"]=>
      string(1) "0"
    }
    ["ns"]=>
    string(10) "work.users"
    ["firstBatch"]=>
    array(4) {
      [0]=>
      array(2) {
        ["_id"]=>
        string(4) "idan"
        ["viewed_cases"]=>
        array(2) { ... }
      }
    }
  }
}

With this implementation, other than the fact that it requires changing all the calls to $results in all of the system, I am worried of unforeseen results in the future considering everything is under "firstBatch". I assume I'll need to build support to obtain the next batches as well, which currently is not the case.

Is there any way to alter the original 3.2-supporting aggregate function above to be 3.4 compliant without changing the response sent to $results?

Thanks in advance

Idan
  • 402
  • 1
  • 5
  • 22
  • Have you tried http://php.net/manual/en/set.mongodb.php? I can't check it now, but the new library could solve your problem. Mongo extension is deprecated – Oleks Dec 02 '16 at 15:03
  • Thanks, but I'm trying to look for a solution that will prevent me from doing the changes I've described above, which would be small compared to introducing an entirely new driver. Also, all documentation I have found so far referred to the mongo driver I am using, which makes it the preferred tool for me to work with. – Idan Dec 02 '16 at 16:30
  • @Idan any solution? – ephramd May 04 '17 at 08:42

0 Answers0