jstests/aggregation/sources/project/remove_redundant_projects.js
/**
 * Helper to test that for a given pipeline, the same results are returned whether or not an
 * index is present.  Also tests whether a projection is absorbed by the pipeline
 * ('expectProjectToCoalesce') and the corresponding project stage ('removedProjectStage') does
 * not exist in the explain output.
 */
Helper to test that for a given pipeline, the same results are returned whether or not an
index is present.  Also tests whether a projection is absorbed by the pipeline
('expectProjectToCoalesce') and the corresponding project stage ('removedProjectStage') does
not exist in the explain output.
Helper to test that for a given pipeline the same results are returned whether or not an index is present . Also tests whether a projection is absorbed by the pipeline ( ' expectProjectToCoalesce ' ) and the corresponding project stage ( ' removedProjectStage ' ) does not exist in the explain output .
function assertResultsMatch({
    pipeline = [],
    expectProjectToCoalesce = false,
    removedProjectStage = null,
    index = indexSpec,
    pipelineOptimizedAway = false
} = {}) {
    pipeline = [{$match: {a: {$gte: 0}}}].concat(pipeline);
    assert.commandWorked(coll.createIndex(index));
    let explain = coll.explain().aggregate(pipeline);
    let resultsWithIndex = coll.aggregate(pipeline).toArray();
    if (!explain.hasOwnProperty("shards")) {
        let result;
        if (pipelineOptimizedAway) {
            assert(isQueryPlan(explain), explain);
            result = getWinningPlan(explain.queryPlanner);
        } else {
            assert(isAggregationPlan(explain), explain);
            result = getWinningPlan(explain.stages[0].$cursor.queryPlanner);
        }
        assert.eq(expectProjectToCoalesce,
                  planHasStage(db, result, "PROJECTION_DEFAULT") ||
                      planHasStage(db, result, "PROJECTION_COVERED") ||
                      planHasStage(db, result, "PROJECTION_SIMPLE"),
                  explain);
        if (!pipelineOptimizedAway) {
            explain.stages.forEach(function(stage) {
                if (stage.hasOwnProperty("$project"))
                    assert.neq(removedProjectStage, stage["$project"], explain);
            });
        }
    }
    assert.commandWorked(coll.dropIndex(index));
    let resultsWithoutIndex = coll.aggregate(pipeline).toArray();
    assert(orderedArrayEq(resultsWithIndex, resultsWithoutIndex));
}
function assertResultsMatch ( { pipeline = [ ] , expectProjectToCoalesce = false , removedProjectStage = null , index = indexSpec , pipelineOptimizedAway = false } = { } ) { pipeline = [ { $match : { a : { $gte : 0 } } } ] . concat ( pipeline ) ; assert . commandWorked ( coll . createIndex ( index ) ) ; let explain = coll . explain ( ) . aggregate ( pipeline ) ; let resultsWithIndex = coll . aggregate ( pipeline ) . toArray ( ) ; if ( ! explain . hasOwnProperty ( "shards" ) ) { let result ; if ( pipelineOptimizedAway ) { assert ( isQueryPlan ( explain ) , explain ) ; result = getWinningPlan ( explain . queryPlanner ) ; } else { assert ( isAggregationPlan ( explain ) , explain ) ; result = getWinningPlan ( explain . stages [ 0 ] . $cursor . queryPlanner ) ; } assert . eq ( expectProjectToCoalesce , planHasStage ( db , result , "PROJECTION_DEFAULT" ) || planHasStage ( db , result , "PROJECTION_COVERED" ) || planHasStage ( db , result , "PROJECTION_SIMPLE" ) , explain ) ; if ( ! pipelineOptimizedAway ) { explain . stages . forEach ( function ( stage ) { if ( stage . hasOwnProperty ( "$project" ) ) assert . neq ( removedProjectStage , stage [ "$project" ] , explain ) ; } ) ; } } assert . commandWorked ( coll . dropIndex ( index ) ) ; let resultsWithoutIndex = coll . aggregate ( pipeline ) . toArray ( ) ; assert ( orderedArrayEq ( resultsWithIndex , resultsWithoutIndex ) ) ; }
Helper to test that for a given pipeline, the same results are returned whether or not an
 index is present.
Helper to test that for a given pipeline the same results are returned whether or not an index is present .
// Add a match stage to ensure index scans are considered for planning (workaround for // SERVER-20066). // Once with an index. // Projection does not get pushed down when sharding filter is used. // Check that $project uses the query system. // Check that $project was removed from pipeline and pushed to the query system. // Again without an index.