jstests/libs/analyze_plan.js
/**
 * Given the root stage of explain's JSON representation of a query plan ('root'), returns true if
 * the query planner reports at least one rejected alternative plan, and false otherwise.
 */
Given the root stage of explain's JSON representation of a query plan ('root'), returns true if
the query planner reports at least one rejected alternative plan, and false otherwise.
Given the root stage of explain ' s JSON representation of a query plan ( ' root ' ) returns true if the query planner reports at least one rejected alternative plan and false otherwise .
function hasRejectedPlans(root) {
    function sectionHasRejectedPlans(explainSection) {
        assert(explainSection.hasOwnProperty("rejectedPlans"), tojson(explainSection));
        return explainSection.rejectedPlans.length !== 0;
    }
    function cursorStageHasRejectedPlans(cursorStage) {
        assert(cursorStage.hasOwnProperty("$cursor"), tojson(cursorStage));
        assert(cursorStage.$cursor.hasOwnProperty("queryPlanner"), tojson(cursorStage));
        return sectionHasRejectedPlans(cursorStage.$cursor.queryPlanner);
    }
    if (root.hasOwnProperty("shards")) {
        const shardExplains = [];
        for (const shard in root.shards) {
            shardExplains.push(root.shards[shard]);
        }
        return shardExplains.some(hasRejectedPlans);
    } else if (root.hasOwnProperty("stages")) {
        const cursorStages = getAggPlanStages(root, "$cursor");
        return cursorStages.find((cursorStage) => cursorStageHasRejectedPlans(cursorStage)) !==
            undefined;
    } else {
        assert(root.hasOwnProperty("queryPlanner"), tojson(root));
        assert(root.queryPlanner.hasOwnProperty("winningPlan"), tojson(root));
        if (!root.queryPlanner.winningPlan.hasOwnProperty("shards")) {
            return sectionHasRejectedPlans(root.queryPlanner);
        }
        return root.queryPlanner.winningPlan.shards.find(
                   (shard) => sectionHasRejectedPlans(shard)) !== undefined;
    }
}
function hasRejectedPlans ( root ) { function sectionHasRejectedPlans ( explainSection ) { assert ( explainSection . hasOwnProperty ( "rejectedPlans" ) , tojson ( explainSection ) ) ; return explainSection . rejectedPlans . length !== 0 ; } function cursorStageHasRejectedPlans ( cursorStage ) { assert ( cursorStage . hasOwnProperty ( "$cursor" ) , tojson ( cursorStage ) ) ; assert ( cursorStage . $cursor . hasOwnProperty ( "queryPlanner" ) , tojson ( cursorStage ) ) ; return sectionHasRejectedPlans ( cursorStage . $cursor . queryPlanner ) ; } if ( root . hasOwnProperty ( "shards" ) ) { const shardExplains = [ ] ; for ( const shard in root . shards ) { shardExplains . push ( root . shards [ shard ] ) ; } return shardExplains . some ( hasRejectedPlans ) ; } else if ( root . hasOwnProperty ( "stages" ) ) { const cursorStages = getAggPlanStages ( root , "$cursor" ) ; return cursorStages . find ( ( cursorStage ) => cursorStageHasRejectedPlans ( cursorStage ) ) !== undefined ; } else { assert ( root . hasOwnProperty ( "queryPlanner" ) , tojson ( root ) ) ; assert ( root . queryPlanner . hasOwnProperty ( "winningPlan" ) , tojson ( root ) ) ; if ( ! root . queryPlanner . winningPlan . hasOwnProperty ( "shards" ) ) { return sectionHasRejectedPlans ( root . queryPlanner ) ; } return root . queryPlanner . winningPlan . shards . find ( ( shard ) => sectionHasRejectedPlans ( shard ) ) !== undefined ; } }
Given the root stage of explain's JSON representation of a query plan ('root'), returns true if
 the query planner reports at least one rejected alternative plan, and false otherwise.
Given the root stage of explain ' s JSON representation of a query plan ( ' root ' ) returns true if the query planner reports at least one rejected alternative plan and false otherwise .
// This is a sharded agg explain. Recursively check whether any of the shards has rejected // plans. // This is an agg explain. // This is some sort of query explain. // This is an unsharded explain. // This is a sharded explain. Each entry in the shards array contains a 'winningPlan' and // 'rejectedPlans'.