@@ -458,4 +458,307 @@ describe('Explain', function () {
458
458
} ) ;
459
459
} )
460
460
} ) ;
461
+
462
+ it ( 'should honor boolean explain with find' , {
463
+ metadata : {
464
+ requires : {
465
+ mongodb : '>=3.0'
466
+ }
467
+ } ,
468
+ test : withClient ( function ( client , done ) {
469
+ const db = client . db ( 'shouldHonorBooleanExplainWithFind' ) ;
470
+ const collection = db . collection ( 'test' ) ;
471
+
472
+ collection . insertOne ( { a : 1 } , ( err , res ) => {
473
+ expect ( err ) . to . not . exist ;
474
+ expect ( res ) . to . exist ;
475
+
476
+ collection . find ( { a : 1 } , { explain : true } ) . toArray ( ( err , docs ) => {
477
+ expect ( err ) . to . not . exist ;
478
+ const explanation = docs [ 0 ] ;
479
+ expect ( explanation ) . to . exist ;
480
+ expect ( explanation ) . property ( 'queryPlanner' ) . to . exist ;
481
+ done ( ) ;
482
+ } ) ;
483
+ } ) ;
484
+ } )
485
+ } ) ;
486
+
487
+ it ( 'should honor string explain with find' , {
488
+ metadata : {
489
+ requires : {
490
+ mongodb : '>=3.0'
491
+ }
492
+ } ,
493
+ test : withClient ( function ( client , done ) {
494
+ const db = client . db ( 'shouldHonorStringExplainWithFind' ) ;
495
+ const collection = db . collection ( 'test' ) ;
496
+
497
+ collection . insertOne ( { a : 1 } , ( err , res ) => {
498
+ expect ( err ) . to . not . exist ;
499
+ expect ( res ) . to . exist ;
500
+
501
+ collection . find ( { a : 1 } , { explain : 'executionStats' } ) . toArray ( ( err , docs ) => {
502
+ expect ( err ) . to . not . exist ;
503
+ const explanation = docs [ 0 ] ;
504
+ expect ( explanation ) . to . exist ;
505
+ expect ( explanation ) . property ( 'queryPlanner' ) . to . exist ;
506
+ expect ( explanation ) . property ( 'executionStats' ) . to . exist ;
507
+ done ( ) ;
508
+ } ) ;
509
+ } ) ;
510
+ } )
511
+ } ) ;
512
+
513
+ it ( 'should honor boolean explain with findOne' , {
514
+ metadata : {
515
+ requires : {
516
+ mongodb : '>=3.0'
517
+ }
518
+ } ,
519
+ test : withClient ( function ( client , done ) {
520
+ const db = client . db ( 'shouldHonorBooleanExplainWithFindOne' ) ;
521
+ const collection = db . collection ( 'test' ) ;
522
+
523
+ collection . insertOne ( { a : 1 } , ( err , res ) => {
524
+ expect ( err ) . to . not . exist ;
525
+ expect ( res ) . to . exist ;
526
+
527
+ collection . findOne ( { a : 1 } , { explain : true } , ( err , explanation ) => {
528
+ expect ( err ) . to . not . exist ;
529
+ expect ( explanation ) . to . exist ;
530
+ expect ( explanation ) . property ( 'queryPlanner' ) . to . exist ;
531
+ done ( ) ;
532
+ } ) ;
533
+ } ) ;
534
+ } )
535
+ } ) ;
536
+
537
+ it ( 'should honor string explain with findOne' , {
538
+ metadata : {
539
+ requires : {
540
+ mongodb : '>=3.0'
541
+ }
542
+ } ,
543
+ test : withClient ( function ( client , done ) {
544
+ const db = client . db ( 'shouldHonorStringExplainWithFindOne' ) ;
545
+ const collection = db . collection ( 'test' ) ;
546
+
547
+ collection . insertOne ( { a : 1 } , ( err , res ) => {
548
+ expect ( err ) . to . not . exist ;
549
+ expect ( res ) . to . exist ;
550
+
551
+ collection . findOne ( { a : 1 } , { explain : 'executionStats' } , ( err , explanation ) => {
552
+ expect ( err ) . to . not . exist ;
553
+ expect ( explanation ) . to . exist ;
554
+ expect ( explanation ) . property ( 'queryPlanner' ) . to . exist ;
555
+ expect ( explanation ) . property ( 'executionStats' ) . to . exist ;
556
+ done ( ) ;
557
+ } ) ;
558
+ } ) ;
559
+ } )
560
+ } ) ;
561
+
562
+ it ( 'should honor boolean explain specified on cursor with find' , {
563
+ metadata : {
564
+ requires : {
565
+ mongodb : '>=3.0'
566
+ }
567
+ } ,
568
+ test : withClient ( function ( client , done ) {
569
+ const db = client . db ( 'shouldHonorBooleanExplainSpecifiedOnCursor' ) ;
570
+ const collection = db . collection ( 'test' ) ;
571
+
572
+ collection . insertOne ( { a : 1 } , ( err , res ) => {
573
+ expect ( err ) . to . not . exist ;
574
+ expect ( res ) . to . exist ;
575
+
576
+ collection . find ( { a : 1 } ) . explain ( false , ( err , explanation ) => {
577
+ expect ( err ) . to . not . exist ;
578
+ expect ( explanation ) . to . exist ;
579
+ expect ( explanation ) . property ( 'queryPlanner' ) . to . exist ;
580
+ done ( ) ;
581
+ } ) ;
582
+ } ) ;
583
+ } )
584
+ } ) ;
585
+
586
+ it ( 'should honor string explain specified on cursor with find' , {
587
+ metadata : {
588
+ requires : {
589
+ mongodb : '>=3.0'
590
+ }
591
+ } ,
592
+ test : withClient ( function ( client , done ) {
593
+ const db = client . db ( 'shouldHonorStringExplainSpecifiedOnCursor' ) ;
594
+ const collection = db . collection ( 'test' ) ;
595
+
596
+ collection . insertOne ( { a : 1 } , ( err , res ) => {
597
+ expect ( err ) . to . not . exist ;
598
+ expect ( res ) . to . exist ;
599
+
600
+ collection . find ( { a : 1 } ) . explain ( 'allPlansExecution' , ( err , explanation ) => {
601
+ expect ( err ) . to . not . exist ;
602
+ expect ( explanation ) . to . exist ;
603
+ expect ( explanation ) . property ( 'queryPlanner' ) . to . exist ;
604
+ expect ( explanation ) . property ( 'executionStats' ) . to . exist ;
605
+ done ( ) ;
606
+ } ) ;
607
+ } ) ;
608
+ } )
609
+ } ) ;
610
+
611
+ it ( 'should honor legacy explain with find' , {
612
+ metadata : {
613
+ requires : {
614
+ mongodb : '<3.0'
615
+ }
616
+ } ,
617
+ test : withClient ( function ( client , done ) {
618
+ const db = client . db ( 'shouldHonorLegacyExplainWithFind' ) ;
619
+ const collection = db . collection ( 'test' ) ;
620
+
621
+ collection . insertOne ( { a : 1 } , ( err , res ) => {
622
+ expect ( err ) . to . not . exist ;
623
+ expect ( res ) . to . exist ;
624
+
625
+ collection . find ( { a : 1 } ) . explain ( ( err , result ) => {
626
+ expect ( err ) . to . not . exist ;
627
+ expect ( result ) . to . have . property ( 'allPlans' ) ;
628
+ done ( ) ;
629
+ } ) ;
630
+ } ) ;
631
+ } )
632
+ } ) ;
633
+
634
+ it (
635
+ 'should honor boolean explain with aggregate' ,
636
+ withClient ( function ( client , done ) {
637
+ const db = client . db ( 'shouldHonorBooleanExplainWithAggregate' ) ;
638
+ const collection = db . collection ( 'test' ) ;
639
+ collection . insertOne ( { a : 1 } , ( err , res ) => {
640
+ expect ( err ) . to . not . exist ;
641
+ expect ( res ) . to . exist ;
642
+
643
+ collection
644
+ . aggregate ( [ { $project : { a : 1 } } , { $group : { _id : '$a' } } ] , { explain : true } )
645
+ . toArray ( ( err , docs ) => {
646
+ expect ( err ) . to . not . exist ;
647
+ const result = docs [ 0 ] ;
648
+ expect ( result ) . to . have . property ( 'stages' ) ;
649
+ expect ( result . stages ) . to . have . lengthOf . at . least ( 1 ) ;
650
+ expect ( result . stages [ 0 ] ) . to . have . property ( '$cursor' ) ;
651
+ done ( ) ;
652
+ } ) ;
653
+ } ) ;
654
+ } )
655
+ ) ;
656
+
657
+ it ( 'should honor string explain with aggregate' , {
658
+ metadata : {
659
+ requires : {
660
+ mongodb : '>=3.6.0'
661
+ }
662
+ } ,
663
+ test : withClient ( function ( client , done ) {
664
+ const db = client . db ( 'shouldHonorStringExplainWithAggregate' ) ;
665
+ const collection = db . collection ( 'test' ) ;
666
+
667
+ collection . insertOne ( { a : 1 } , ( err , res ) => {
668
+ expect ( err ) . to . not . exist ;
669
+ expect ( res ) . to . exist ;
670
+
671
+ collection
672
+ . aggregate ( [ { $project : { a : 1 } } , { $group : { _id : '$a' } } ] , {
673
+ explain : 'executionStats'
674
+ } )
675
+ . toArray ( ( err , docs ) => {
676
+ expect ( err ) . to . not . exist ;
677
+ const result = docs [ 0 ] ;
678
+ expect ( result ) . to . have . property ( 'stages' ) ;
679
+ expect ( result . stages ) . to . have . lengthOf . at . least ( 1 ) ;
680
+ expect ( result . stages [ 0 ] ) . to . have . property ( '$cursor' ) ;
681
+ expect ( result . stages [ 0 ] . $cursor ) . to . have . property ( 'queryPlanner' ) ;
682
+ expect ( result . stages [ 0 ] . $cursor ) . to . have . property ( 'executionStats' ) ;
683
+ done ( ) ;
684
+ } ) ;
685
+ } ) ;
686
+ } )
687
+ } ) ;
688
+
689
+ it (
690
+ 'should honor boolean explain specified on cursor with aggregate' ,
691
+ withClient ( function ( client , done ) {
692
+ const db = client . db ( 'shouldHonorBooleanExplainSpecifiedOnCursor' ) ;
693
+ const collection = db . collection ( 'test' ) ;
694
+
695
+ collection . insertOne ( { a : 1 } , ( err , res ) => {
696
+ expect ( err ) . to . not . exist ;
697
+ expect ( res ) . to . exist ;
698
+
699
+ collection
700
+ . aggregate ( [ { $project : { a : 1 } } , { $group : { _id : '$a' } } ] )
701
+ . explain ( false , ( err , result ) => {
702
+ expect ( err ) . to . not . exist ;
703
+ expect ( result ) . to . have . property ( 'stages' ) ;
704
+ expect ( result . stages ) . to . have . lengthOf . at . least ( 1 ) ;
705
+ expect ( result . stages [ 0 ] ) . to . have . property ( '$cursor' ) ;
706
+ done ( ) ;
707
+ } ) ;
708
+ } ) ;
709
+ } )
710
+ ) ;
711
+
712
+ it ( 'should honor string explain specified on cursor with aggregate' , {
713
+ metadata : {
714
+ requires : {
715
+ mongodb : '>=3.6'
716
+ }
717
+ } ,
718
+ test : withClient ( function ( client , done ) {
719
+ const db = client . db ( 'shouldHonorStringExplainSpecifiedOnCursor' ) ;
720
+ const collection = db . collection ( 'test' ) ;
721
+
722
+ collection . insertOne ( { a : 1 } , ( err , res ) => {
723
+ expect ( err ) . to . not . exist ;
724
+ expect ( res ) . to . exist ;
725
+
726
+ collection
727
+ . aggregate ( [ { $project : { a : 1 } } , { $group : { _id : '$a' } } ] )
728
+ . explain ( 'allPlansExecution' , ( err , result ) => {
729
+ expect ( err ) . to . not . exist ;
730
+ expect ( result ) . to . exist ;
731
+ expect ( result ) . to . have . property ( 'stages' ) ;
732
+ expect ( result . stages ) . to . have . lengthOf . at . least ( 1 ) ;
733
+ expect ( result . stages [ 0 ] ) . to . have . property ( '$cursor' ) ;
734
+ expect ( result . stages [ 0 ] . $cursor ) . to . have . property ( 'queryPlanner' ) ;
735
+ expect ( result . stages [ 0 ] . $cursor ) . to . have . property ( 'executionStats' ) ;
736
+ done ( ) ;
737
+ } ) ;
738
+ } ) ;
739
+ } )
740
+ } ) ;
741
+
742
+ it (
743
+ 'should honor legacy explain with aggregate' ,
744
+ withClient ( function ( client , done ) {
745
+ const db = client . db ( 'shouldHonorLegacyExplainWithAggregate' ) ;
746
+ const collection = db . collection ( 'test' ) ;
747
+
748
+ collection . insertOne ( { a : 1 } , ( err , res ) => {
749
+ expect ( err ) . to . not . exist ;
750
+ expect ( res ) . to . exist ;
751
+
752
+ collection
753
+ . aggregate ( [ { $project : { a : 1 } } , { $group : { _id : '$a' } } ] )
754
+ . explain ( ( err , result ) => {
755
+ expect ( err ) . to . not . exist ;
756
+ expect ( result ) . to . have . property ( 'stages' ) ;
757
+ expect ( result . stages ) . to . have . lengthOf . at . least ( 1 ) ;
758
+ expect ( result . stages [ 0 ] ) . to . have . property ( '$cursor' ) ;
759
+ done ( ) ;
760
+ } ) ;
761
+ } ) ;
762
+ } )
763
+ ) ;
461
764
} ) ;
0 commit comments