jstests/core/sort_array.js
/**
 * Runs a $match-$sort-$project query as both a find and then an aggregate. Asserts that the
 * result set, after being converted to an array, is equal to 'expected'. Also asserts that the
 * find plan either uses the SORT stage and the agg plan uses the "$sort" agg stage or does not use
 * either dependent on the value of expectBlockingSort.
 */
Runs a $match-$sort-$project query as both a find and then an aggregate. Asserts that the
result set, after being converted to an array, is equal to 'expected'. Also asserts that the
find plan either uses the SORT stage and the agg plan uses the "$sort" agg stage or does not use
either dependent on the value of expectBlockingSort.
Runs a $match - $sort - $project query as both a find and then an aggregate . Asserts that the result set after being converted to an array is equal to ' expected ' . Also asserts that the find plan either uses the SORT stage and the agg plan uses the " $sort " agg stage or does not use either dependent on the value of expectBlockingSort .
function testAggAndFindSort({filter, sort, project, hint, expected, expectBlockingSort}) {
    let cursor = coll.find(filter, project).sort(sort);
    assert.eq(cursor.toArray(), expected);
    if (hint) {
        cursor = coll.find(filter, project).sort(sort).hint(hint);
        assert.eq(cursor.toArray(), expected);
    }
    let explain = coll.find(filter, project).sort(sort).explain();
    if (expectBlockingSort) {
        assert(planHasStage(db, explain, "SORT"));
    } else {
        assert(!planHasStage(db, explain, "SORT"));
    }
    let pipeline = [
        {$_internalInhibitOptimization: {}},
        {$match: filter},
        {$sort: sort},
        {$project: project},
    ];
    cursor = coll.aggregate(pipeline);
    assert.eq(cursor.toArray(), expected);
    explain = coll.explain().aggregate(pipeline);
    assert(aggPlanHasStage(explain, "$sort"));
}
function testAggAndFindSort ( { filter , sort , project , hint , expected , expectBlockingSort } ) { let cursor = coll . find ( filter , project ) . sort ( sort ) ; assert . eq ( cursor . toArray ( ) , expected ) ; if ( hint ) { cursor = coll . find ( filter , project ) . sort ( sort ) . hint ( hint ) ; assert . eq ( cursor . toArray ( ) , expected ) ; } let explain = coll . find ( filter , project ) . sort ( sort ) . explain ( ) ; if ( expectBlockingSort ) { assert ( planHasStage ( db , explain , "SORT" ) ) ; } else { assert ( ! planHasStage ( db , explain , "SORT" ) ) ; } let pipeline = [ { $_internalInhibitOptimization : { } } , { $match : filter } , { $sort : sort } , { $project : project } , ] ; cursor = coll . aggregate ( pipeline ) ; assert . eq ( cursor . toArray ( ) , expected ) ; explain = coll . explain ( ) . aggregate ( pipeline ) ; assert ( aggPlanHasStage ( explain , "$sort" ) ) ; }
Runs a $match-$sort-$project query as both a find and then an aggregate.
Runs a $match - $sort - $project query as both a find and then an aggregate .
// If there was a hint specified, make sure we get the same results with the hint.