Starting with MongoDB 3.2.0, aggregations which require a merging shard randomly select a merger amongst the shards participating in the query. This behavior was introduced under
SERVER-18925 in order to help evenly distribute query load. In many situations this behavior is helpful, but it can have drawbacks. For example, when additional shards take on a merging role, more intra-cluster connections are required, which in turn requires additional resources. Therefore, we should introduce a setParameter on mongos called internalQueryAlwaysMergeOnPrimaryShard which can be used to disable random merger selection. The parameter's value is false by default. When configured to true, all aggregations which require a merging shard will merge on the primary shard. Pipelines which do not split into a shards part and merger part are not affected.
The parameter can be set on the command line when starting mongos or via a config file. To set it using the shell, connect to mongos and run the following:
Note that this parameter is for mongos only; attempting to set it against a mongod will result in an error: