The eval command on mongod calls into the js scripting engine.
Since eval() is called on a database, not a namespace, it does not check the shardVersion sent in the request (or the shardVersion for the connection) directly.
Instead, any time a namespace is accessed by the script, the scripting engine checks that the namespace is not sharded (in DBInfo::getProperty() and DBCollectionInfo::construct).
It does this check by calling ShardingState::haveLocalShardingInfo():
bool haveLocalShardingInfo(OperationContext* opCtx, const string& ns) { if (!ShardingState::get(opCtx)->enabled()) { return false; } const auto& oss = OperationShardingState::get(opCtx); if (oss.hasShardVersion()) { return true; } const auto& sci = ShardedConnectionInfo::get(opCtx->getClient(), false); if (sci && !sci->getVersion(ns).isStrictlyEqualTo(ChunkVersion::UNSHARDED())) { return true; } return false; }
This function assumes the collection is sharded if:
1) the OperationShardingState has a shardVersion (aka, a shardVersion was attached to the command in the request)
2) the connection used for sending the eval() command previously set a shardVersion for the namespace.
The mongos EvalCmd, which forwards the command to the primary shard for the database eval() was run on, does not attach a shardVersion to the cmdObj.
On the other hand, the passthrough() function called by mongos's EvalCmd uses ShardConnection on an empty namespace.
Therefore, only if the connection mongos gets from the pool had previously set a shardVersion for a namespace accessed by the script (e.g., due to a different, prior command), will the shard host have a ShardedConnectionInfo with a non-UNSHARDED version for the namespace, and only in this case will the haveLocalShardingInfo() check on mongod work if the collection is sharded.