3
3
namespace MongoDB \Tests \Operation ;
4
4
5
5
use MongoDB \Driver \BulkWrite ;
6
+ use MongoDB \Operation \Aggregate ;
6
7
use MongoDB \Operation \Count ;
7
8
use MongoDB \Operation \CreateCollection ;
8
9
use MongoDB \Operation \Delete ;
@@ -387,6 +388,43 @@ public function testUpdateOne($verbosity, $executionStatsExpected, $allPlansExec
387
388
$ this ->assertExplainResult ($ result , $ executionStatsExpected , $ allPlansExecutionExpected );
388
389
}
389
390
391
+ public function testAggregate ()
392
+ {
393
+ if (version_compare ($ this ->getServerVersion (), '4.0.0 ' , '< ' )) {
394
+ $ this ->markTestSkipped ('Explaining aggregate command requires server version >= 4.0 ' );
395
+ }
396
+
397
+ $ this ->createFixtures (3 );
398
+
399
+ $ pipeline = [['$group ' => ['_id ' => null ]]];
400
+ $ operation = new Aggregate ($ this ->getDatabaseName (), $ this ->getCollectionName (), $ pipeline );
401
+
402
+ $ explainOperation = new Explain ($ this ->getDatabaseName (), $ operation , ['verbosity ' => Explain::VERBOSITY_QUERY , 'typeMap ' => ['root ' => 'array ' , 'document ' => 'array ' ]]);
403
+ $ result = $ explainOperation ->execute ($ this ->getPrimaryServer ());
404
+
405
+ $ this ->assertExplainResult ($ result , false , false , true );
406
+ }
407
+
408
+ /**
409
+ * @dataProvider provideVerbosityInformation
410
+ */
411
+ public function testAggregateOptimizedToQuery ($ verbosity , $ executionStatsExpected , $ allPlansExecutionExpected )
412
+ {
413
+ if (version_compare ($ this ->getServerVersion (), '4.2.0 ' , '< ' )) {
414
+ $ this ->markTestSkipped ('MongoDB < 4.2 does not optimize simple aggregation pipelines ' );
415
+ }
416
+
417
+ $ this ->createFixtures (3 );
418
+
419
+ $ pipeline = [['$match ' => ['_id ' => ['$ne ' => 2 ]]]];
420
+ $ operation = new Aggregate ($ this ->getDatabaseName (), $ this ->getCollectionName (), $ pipeline );
421
+
422
+ $ explainOperation = new Explain ($ this ->getDatabaseName (), $ operation , ['verbosity ' => $ verbosity , 'typeMap ' => ['root ' => 'array ' , 'document ' => 'array ' ]]);
423
+ $ result = $ explainOperation ->execute ($ this ->getPrimaryServer ());
424
+
425
+ $ this ->assertExplainResult ($ result , $ executionStatsExpected , $ allPlansExecutionExpected );
426
+ }
427
+
390
428
public function provideVerbosityInformation ()
391
429
{
392
430
return [
@@ -396,9 +434,14 @@ public function provideVerbosityInformation()
396
434
];
397
435
}
398
436
399
- private function assertExplainResult ($ result , $ executionStatsExpected , $ allPlansExecutionExpected )
437
+ private function assertExplainResult ($ result , $ executionStatsExpected , $ allPlansExecutionExpected, $ stagesExpected = false )
400
438
{
401
- $ this ->assertArrayHasKey ('queryPlanner ' , $ result );
439
+ if ($ stagesExpected ) {
440
+ $ this ->assertArrayHasKey ('stages ' , $ result );
441
+ } else {
442
+ $ this ->assertArrayHasKey ('queryPlanner ' , $ result );
443
+ }
444
+
402
445
if ($ executionStatsExpected ) {
403
446
$ this ->assertArrayHasKey ('executionStats ' , $ result );
404
447
if ($ allPlansExecutionExpected ) {
0 commit comments