jstests/sharding/query/agg_mongos_merge.js
/**
 * Runs the aggregation specified by 'pipeline', verifying that:
 * - The number of documents returned by the aggregation matches 'expectedCount'.
 * - The merge was performed on a mongoS if 'mergeType' is 'mongos', and on a shard otherwise.
 */
Runs the aggregation specified by 'pipeline', verifying that:
- The number of documents returned by the aggregation matches 'expectedCount'.
- The merge was performed on a mongoS if 'mergeType' is 'mongos', and on a shard otherwise.
Runs the aggregation specified by ' pipeline ' verifying that : - The number of documents returned by the aggregation matches ' expectedCount ' . - The merge was performed on a mongoS if ' mergeType ' is ' mongos ' and on a shard otherwise .
function assertMergeBehaviour(
    {testName, pipeline, mergeType, batchSize, allowDiskUse, expectedCount}) {
    assert(!testNameHistory.has(testName));
    testNameHistory.add(testName);
    const opts = {
        comment: testName,
        cursor: (batchSize ? {batchSize: batchSize} : {}),
    };
    if (allowDiskUse !== undefined) {
        opts.allowDiskUse = allowDiskUse;
    }
    assert.eq(
        assert.commandWorked(mongosColl.explain().aggregate(pipeline, Object.extend({}, opts)))
            .mergeType,
        mergeType);
    assert.eq(mongosColl.aggregate(pipeline, opts).itcount(), expectedCount);
    profilerHasNumMatchingEntriesOrThrow({
        profileDB: primaryShardDB,
        numExpectedMatches: (mergeType === "mongos" ? 0 : 1),
        filter: {
            "command.aggregate": mongosColl.getName(),
            "command.comment": testName,
            "command.pipeline.$mergeCursors": {$exists: 1}
        }
    });
}
function assertMergeBehaviour ( { testName , pipeline , mergeType , batchSize , allowDiskUse , expectedCount } ) { assert ( ! testNameHistory . has ( testName ) ) ; testNameHistory . add ( testName ) ; const opts = { comment : testName , cursor : ( batchSize ? { batchSize : batchSize } : { } ) , } ; if ( allowDiskUse !== undefined ) { opts . allowDiskUse = allowDiskUse ; } assert . eq ( assert . commandWorked ( mongosColl . explain ( ) . aggregate ( pipeline , Object . extend ( { } , opts ) ) ) . mergeType , mergeType ) ; assert . eq ( mongosColl . aggregate ( pipeline , opts ) . itcount ( ) , expectedCount ) ; profilerHasNumMatchingEntriesOrThrow ( { profileDB : primaryShardDB , numExpectedMatches : ( mergeType === "mongos" ? 0 : 1 ) , filter : { "command.aggregate" : mongosColl . getName ( ) , "command.comment" : testName , "command.pipeline.$mergeCursors" : { $exists : 1 } } } ) ; }
Runs the aggregation specified by 'pipeline', verifying that:
 - The number of documents returned by the aggregation matches 'expectedCount'.
Runs the aggregation specified by ' pipeline ' verifying that : - The number of documents returned by the aggregation matches ' expectedCount ' .
// Ensure that this test has a unique name. // Create the aggregation options from the given arguments. // Verify that the explain() output's 'mergeType' field matches our expectation. // Verify that the aggregation returns the expected number of results. // Verify that a $mergeCursors aggregation ran on the primary shard if 'mergeType' is not // 'mongos', and that no such aggregation ran otherwise.