jstests/sharding/query/agg_mongos_merge.js
/**
 * Runs a series of test cases which will always merge on mongoD when 'allowDiskUse' is true,
 * and on mongoS when 'allowDiskUse' is false or omitted.
 */
Runs a series of test cases which will always merge on mongoD when 'allowDiskUse' is true,
and on mongoS when 'allowDiskUse' is false or omitted.
Runs a series of test cases which will always merge on mongoD when ' allowDiskUse ' is true and on mongoS when ' allowDiskUse ' is false or omitted .
function runTestCasesWhoseMergeLocationDependsOnAllowDiskUse(allowDiskUse) {
    const assertMergeOnMongoX = (allowDiskUse ? assertMergeOnMongoD : assertMergeOnMongoS);
    assertMergeOnMongoX({
        testName: "agg_mongos_merge_group_allow_disk_use",
        pipeline:
            [{$match: {_id: {$gte: -200, $lte: 200}}}, {$group: {_id: {$mod: ["$_id", 150]}}}],
        allowDiskUse: allowDiskUse,
        expectedCount: 299
    });
    assertMergeOnMongoX({
        testName: "agg_mongos_merge_blocking_sort_allow_disk_use",
        pipeline: [
            {$match: {_id: {$gte: -200, $lte: 200}}},
            {$sort: {_id: 1}},
            {$_internalSplitPipeline: {}},
            {$sort: {a: 1}}
        ],
        allowDiskUse: allowDiskUse,
        expectedCount: 400
    });
    assertMergeOnMongoX({
        testName: "agg_mongos_merge_blocking_sample_allow_disk_use",
        pipeline: [
            {$match: {_id: {$gte: -200, $lte: 200}}},
            {$sample: {size: 300}},
            {$sample: {size: 200}}
        ],
        allowDiskUse: allowDiskUse,
        expectedCount: 200
    });
    assertMergeOnMongoX({
        testName: "agg_mongos_merge_facet_allow_disk_use",
        pipeline: [
            {$match: {_id: {$gte: -200, $lte: 200}}},
            {
                $facet: {
                    pipe1: [{$match: {_id: {$gt: 0}}}, {$skip: 10}, {$limit: 150}],
                    pipe2: [{$match: {_id: {$lt: 0}}}, {$sort: {a: -1}}]
                }
            }
        ],
        allowDiskUse: allowDiskUse,
        expectedCount: 1
    });
    assertMergeOnMongoX({
        testName: "agg_mongos_merge_bucket_auto_allow_disk_use",
        pipeline: [
            {$match: {_id: {$gte: -200, $lte: 200}}},
            {$bucketAuto: {groupBy: "$_id", buckets: 10}}
        ],
        allowDiskUse: allowDiskUse,
        expectedCount: 10
    });
    if (isShardedLookupEnabled) {
        assertMergeOnMongoX({
            testName: "agg_mongos_merge_lookup_subpipeline_disk_use_" + allowDiskUse,
            pipeline: [
                {$match: {_id: {$gt: -25, $lte: 25}}},
                {$_internalSplitPipeline: {}},
                {
                    $lookup: {
                        from: mongosColl.getName(),
                        let: {idVar: "$_id"},
                        pipeline: [{$group: {_id: {$mod: ["$_id", 150]}}}],
                        as: "lookupField"
                    }
                }
            ],
            allowDiskUse: allowDiskUse,
            expectedCount: 50
        });
    }
     Test composite stages.
     Test that $bucket ($group->$sort) is merged on mongoS iff 'allowDiskUse' is not set.
    assertMergeOnMongoX({
        testName: "agg_mongos_merge_bucket_allow_disk_use",
        pipeline: [
            {$match: {_id: {$gte: -200, $lte: 200}}},
            {$bucket: {groupBy: "$_id", boundaries: [-200, -150, -100, -50, 0, 50, 100, 150, 200]}}
        ],
        allowDiskUse: allowDiskUse,
        expectedCount: 8
    });
     Test that $sortByCount ($group->$sort) is merged on mongoS iff 'allowDiskUse' isn't set.
    assertMergeOnMongoX({
        testName: "agg_mongos_merge_sort_by_count_allow_disk_use",
        pipeline: [{$match: {_id: {$gte: -200, $lte: 200}}}, {$sortByCount: {$mod: ["$_id", 150]}}],
        allowDiskUse: allowDiskUse,
        expectedCount: 299
    });
     Test that $count ($group->$project) is merged on mongoS iff 'allowDiskUse' is not set.
    assertMergeOnMongoX({
        testName: "agg_mongos_merge_count_allow_disk_use",
        pipeline: [{$match: {_id: {$gte: -150, $lte: 1500}}}, {$count: "doc_count"}],
        allowDiskUse: allowDiskUse,
        expectedCount: 1
    });
}
function runTestCasesWhoseMergeLocationDependsOnAllowDiskUse ( allowDiskUse ) { const assertMergeOnMongoX = ( allowDiskUse ? assertMergeOnMongoD : assertMergeOnMongoS ) ; assertMergeOnMongoX ( { testName : "agg_mongos_merge_group_allow_disk_use" , pipeline : [ { $match : { _id : { $gte : - 200 , $lte : 200 } } } , { $group : { _id : { $mod : [ "$_id" , 150 ] } } } ] , allowDiskUse : allowDiskUse , expectedCount : 299 } ) ; assertMergeOnMongoX ( { testName : "agg_mongos_merge_blocking_sort_allow_disk_use" , pipeline : [ { $match : { _id : { $gte : - 200 , $lte : 200 } } } , { $sort : { _id : 1 } } , { $_internalSplitPipeline : { } } , { $sort : { a : 1 } } ] , allowDiskUse : allowDiskUse , expectedCount : 400 } ) ; assertMergeOnMongoX ( { testName : "agg_mongos_merge_blocking_sample_allow_disk_use" , pipeline : [ { $match : { _id : { $gte : - 200 , $lte : 200 } } } , { $sample : { size : 300 } } , { $sample : { size : 200 } } ] , allowDiskUse : allowDiskUse , expectedCount : 200 } ) ; assertMergeOnMongoX ( { testName : "agg_mongos_merge_facet_allow_disk_use" , pipeline : [ { $match : { _id : { $gte : - 200 , $lte : 200 } } } , { $facet : { pipe1 : [ { $match : { _id : { $gt : 0 } } } , { $skip : 10 } , { $limit : 150 } ] , pipe2 : [ { $match : { _id : { $lt : 0 } } } , { $sort : { a : - 1 } } ] } } ] , allowDiskUse : allowDiskUse , expectedCount : 1 } ) ; assertMergeOnMongoX ( { testName : "agg_mongos_merge_bucket_auto_allow_disk_use" , pipeline : [ { $match : { _id : { $gte : - 200 , $lte : 200 } } } , { $bucketAuto : { groupBy : "$_id" , buckets : 10 } } ] , allowDiskUse : allowDiskUse , expectedCount : 10 } ) ; if ( isShardedLookupEnabled ) { assertMergeOnMongoX ( { testName : "agg_mongos_merge_lookup_subpipeline_disk_use_" + allowDiskUse , pipeline : [ { $match : { _id : { $gt : - 25 , $lte : 25 } } } , { $_internalSplitPipeline : { } } , { $lookup : { from : mongosColl . getName ( ) , let : { idVar : "$_id" } , pipeline : [ { $group : { _id : { $mod : [ "$_id" , 150 ] } } } ] , as : "lookupField" } } ] , allowDiskUse : allowDiskUse , expectedCount : 50 } ) ; } assertMergeOnMongoX ( { testName : "agg_mongos_merge_bucket_allow_disk_use" , pipeline : [ { $match : { _id : { $gte : - 200 , $lte : 200 } } } , { $bucket : { groupBy : "$_id" , boundaries : [ - 200 , - 150 , - 100 , - 50 , 0 , 50 , 100 , 150 , 200 ] } } ] , allowDiskUse : allowDiskUse , expectedCount : 8 } ) ; assertMergeOnMongoX ( { testName : "agg_mongos_merge_sort_by_count_allow_disk_use" , pipeline : [ { $match : { _id : { $gte : - 200 , $lte : 200 } } } , { $sortByCount : { $mod : [ "$_id" , 150 ] } } ] , allowDiskUse : allowDiskUse , expectedCount : 299 } ) ; assertMergeOnMongoX ( { testName : "agg_mongos_merge_count_allow_disk_use" , pipeline : [ { $match : { _id : { $gte : - 150 , $lte : 1500 } } } , { $count : "doc_count" } ] , allowDiskUse : allowDiskUse , expectedCount : 1 } ) ; }
Runs a series of test cases which will always merge on mongoD when 'allowDiskUse' is true,
 and on mongoS when 'allowDiskUse' is false or omitted.
Runs a series of test cases which will always merge on mongoD when ' allowDiskUse ' is true and on mongoS when ' allowDiskUse ' is false or omitted .
// All test cases should merge on mongoD if allowDiskUse is true, mongoS otherwise. // Test that $group is only merged on mongoS if 'allowDiskUse' is not set. // Adjacent $sort stages will be coalesced and merge sort will occur on anyShard when disk use // is allowed, and on mongos otherwise. // Test that a blocking $sample is only merged on mongoS if 'allowDiskUse' is not set. // Test that $facet is only merged on mongoS if all pipelines are mongoS-mergeable when // 'allowDiskUse' is not set. // Test that $bucketAuto is only merged on mongoS if 'allowDiskUse' is not set. // Test that $lookup with a subpipeline containing a spillable stage is only merged on // mongoS when 'allowDiskUse' is not set. // $lookup is now allowed on the shards pipeline, but we should force it to be part // of the merge pipeline for this test. // // Test composite stages. // // Test that $bucket ($group->$sort) is merged on mongoS iff 'allowDiskUse' is not set. // Test that $sortByCount ($group->$sort) is merged on mongoS iff 'allowDiskUse' isn't set. // Test that $count ($group->$project) is merged on mongoS iff 'allowDiskUse' is not set.