jstests/aggregation/explain_limit.js
// the value of the limit.
the value of the limit.
the value of the limit .
function checkResults({results, verbosity}) {
    const [cursorSubdocs, limitAmount] = (() => {
        if (verbosity != "queryPlanner" && isSBEEnabled) {
            const useQueryPlannerSection = true;
            const queryPlannerStages = getAggPlanStages(results, "LIMIT", useQueryPlannerSection);
            const execStatsStages = getAggPlanStages(results, "limit");
            assert.gt(queryPlannerStages.length, 0, results);
            assert.gt(execStatsStages.length, 0, results);
            const planNodeIds = new Set(queryPlannerStages.map(stage => stage.planNodeId));
            return [execStatsStages.filter(stage => planNodeIds.has(stage.planNodeId)), "limit"];
        }
        return [getAggPlanStages(results, "LIMIT"), "limitAmount"];
    })();
    assert.gt(cursorSubdocs.length, 0, results);
    for (let stageResult of cursorSubdocs) {
        assert.eq(stageResult[limitAmount], NumberLong(kLimit), cursorSubdocs);
        if (verbosity !== "queryPlanner") {
            assert.eq(stageResult.nReturned, NumberLong(kLimit), results);
        }
    }
    if (verbosity !== "queryPlanner") {
        if (results.hasOwnProperty("executionStats")) {
            assert.eq(results.executionStats.nReturned, kLimit, results);
            assert.eq(results.executionStats.totalDocsExamined, kLimit, results);
        } else {
            assert(results.hasOwnProperty("shards"));
            for (let elem in results.shards) {
                const shardExecStats = results.shards[elem].executionStats;
                assert.eq(shardExecStats.nReturned, kLimit, results);
                assert.eq(shardExecStats.totalDocsExamined, kLimit, results);
            }
        }
    }
}
function checkResults ( { results , verbosity } ) { const [ cursorSubdocs , limitAmount ] = ( ( ) => { if ( verbosity != "queryPlanner" && isSBEEnabled ) { const useQueryPlannerSection = true ; const queryPlannerStages = getAggPlanStages ( results , "LIMIT" , useQueryPlannerSection ) ; const execStatsStages = getAggPlanStages ( results , "limit" ) ; assert . gt ( queryPlannerStages . length , 0 , results ) ; assert . gt ( execStatsStages . length , 0 , results ) ; const planNodeIds = new Set ( queryPlannerStages . map ( stage => stage . planNodeId ) ) ; return [ execStatsStages . filter ( stage => planNodeIds . has ( stage . planNodeId ) ) , "limit" ] ; } return [ getAggPlanStages ( results , "LIMIT" ) , "limitAmount" ] ; } ) ( ) ; assert . gt ( cursorSubdocs . length , 0 , results ) ; for ( let stageResult of cursorSubdocs ) { assert . eq ( stageResult [ limitAmount ] , NumberLong ( kLimit ) , cursorSubdocs ) ; if ( verbosity !== "queryPlanner" ) { assert . eq ( stageResult . nReturned , NumberLong ( kLimit ) , results ) ; } } if ( verbosity !== "queryPlanner" ) { if ( results . hasOwnProperty ( "executionStats" ) ) { assert . eq ( results . executionStats . nReturned , kLimit , results ) ; assert . eq ( results . executionStats . totalDocsExamined , kLimit , results ) ; } else { assert ( results . hasOwnProperty ( "shards" ) ) ; for ( let elem in results . shards ) { const shardExecStats = results . shards [ elem ] . executionStats ; assert . eq ( shardExecStats . nReturned , kLimit , results ) ; assert . eq ( shardExecStats . totalDocsExamined , kLimit , results ) ; } } } }
the value of the limit.
the value of the limit .
// We cannot use the "executionStats" section for SBE plans without some pre-processing, // since it has different explain format. To find execution stats for the LIMIT stages // from the "queryPlanner" section (there could be multiple of such stages if we're in a // sharded environment, one for each shard) we first extract their 'planNodeIds' into a // set. Then we filter out all "limit" stages from the "executionStats" section by // keeping only correlated plan stages, and return the final array. Since the name of // the field holding the limit amount also differs in SBE, we return a proper name as // well. // Explain should report that we only have to examine as many documents as the limit. // This must be output for a sharded explain. Verify that each shard reports the // expected execution stats.