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