1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace MongoDB\Tests\Collection\CrudSpec;
use MongoDB\Collection;
use MongoDB\Driver\ReadPreference;
use MongoDB\Operation\DropCollection;
/**
* CRUD spec functional tests for aggregate().
*
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
*/
class AggregateFunctionalTest extends FunctionalTestCase
{
private static $wireVersionForOutOperator = 2;
public function setUp()
{
parent::setUp();
$this->createFixtures(3);
}
public function testAggregateWithMultipleStages()
{
$cursor = $this->collection->aggregate(
[
['$sort' => ['x' => 1]],
['$match' => ['_id' => ['$gt' => 1]]],
],
['batchSize' => 2]
);
$expected = [
['_id' => 2, 'x' => 22],
['_id' => 3, 'x' => 33],
];
$this->assertSameDocuments($expected, $cursor);
}
public function testAggregateWithOut()
{
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
if ( ! \MongoDB\server_supports_feature($server, self::$wireVersionForOutOperator)) {
$this->markTestSkipped('$out aggregation pipeline operator is not supported');
}
$outputCollection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName() . '_output');
$operation = new DropCollection($this->getDatabaseName(), $outputCollection->getCollectionName());
$operation->execute($this->getPrimaryServer());
$this->collection->aggregate(
[
['$sort' => ['x' => 1]],
['$match' => ['_id' => ['$gt' => 1]]],
['$out' => $outputCollection->getCollectionName()],
]
);
$expected = [
['_id' => 2, 'x' => 22],
['_id' => 3, 'x' => 33],
];
$this->assertSameDocuments($expected, $outputCollection->find());
// Manually clean up our output collection
$operation = new DropCollection($this->getDatabaseName(), $outputCollection->getCollectionName());
$operation->execute($this->getPrimaryServer());
}
public function testAggregateWithoutCursorDoesNotApplyTypemap()
{
$pipeline = [
['$group' => [
'_id' => null,
'count' => ['$sum' => 1]
]]
];
$options = ['useCursor' => false];
$result = $this->collection->aggregate($pipeline, $options);
$expected = [
[
'_id' => null,
'count' => 3,
],
];
$this->assertSameDocuments($expected, $result);
}
}