jstests/noPassthrough/agg_collstats_expr.js
/* Accepts a dbName, collName, and shardDistribution (array of positive integers or nulls).
 * Creates a sharded cluster with shardDistribution.length shards and shardDistribution[i] documents
 * on the i-th shard or no chunks assigned to that shard if shardDistribution[i] is null.
 */
Accepts a dbName, collName, and shardDistribution (array of positive integers or nulls).
Creates a sharded cluster with shardDistribution.length shards and shardDistribution[i] documents
on the i-th shard or no chunks assigned to that shard if shardDistribution[i] is null.
Accepts a dbName collName and shardDistribution ( array of positive integers or nulls ) . Creates a sharded cluster with shardDistribution . length shards and shardDistribution [ i ] documents on the i - th shard or no chunks assigned to that shard if shardDistribution [ i ] is null .
function runShardingTestExists(shardDistribution) {
    const st = ShardingTest({shards: shardDistribution.length});
    const mongos = st.s0;
    const admin = mongos.getDB("admin");
    const config = mongos.getDB("config");
    const shards = config.shards.find().toArray();
    const namespace = dbName + "." + collName;
    assert.commandWorked(admin.runCommand({enableSharding: dbName}));
    assert.commandWorked(admin.runCommand({movePrimary: dbName, to: shards[0]._id}));
    assert.commandWorked(admin.runCommand({shardCollection: namespace, key: {a: 1}}));
    const coll = mongos.getCollection(namespace);
    const length = shardDistribution.length;
    let curr = 0;
    let startChunk = curr;
    for (let i = 0; i < length; i++) {
        for (startChunk = curr;
             shardDistribution[i] != null && curr < startChunk + shardDistribution[i];
             curr++) {
            assert.commandWorked(coll.insert({a: curr}));
        }
        if (shardDistribution[i] == 0) {
            curr++;
        }
        if (shardDistribution[i] != null) {
            assert.commandWorked(st.splitAt(namespace, {a: curr}));
        }
        assert.commandWorked(admin.runCommand(
            {moveChunk: namespace, find: {a: curr + 1}, to: shards[(i + 1) % length]._id}));
    }
    for (let j = 0; shardDistribution[j] == null && j < length; j++)
        assert.commandWorked(
            admin.runCommand({moveChunk: namespace, find: {a: curr + 1}, to: shards[j + 1]._id}));
    const counts = coll.aggregate([{"$collStats": {"count": {}}}]).toArray();
    for (let i = 0; i < shards.length; i++) {
        assert.eq(getShardCount(counts, shards[i]._id)["count"], shardDistribution[i]);
    }
    st.stop();
}
function runShardingTestExists ( shardDistribution ) { const st = ShardingTest ( { shards : shardDistribution . length } ) ; const mongos = st . s0 ; const admin = mongos . getDB ( "admin" ) ; const config = mongos . getDB ( "config" ) ; const shards = config . shards . find ( ) . toArray ( ) ; const namespace = dbName + "." + collName ; assert . commandWorked ( admin . runCommand ( { enableSharding : dbName } ) ) ; assert . commandWorked ( admin . runCommand ( { movePrimary : dbName , to : shards [ 0 ] . _id } ) ) ; assert . commandWorked ( admin . runCommand ( { shardCollection : namespace , key : { a : 1 } } ) ) ; const coll = mongos . getCollection ( namespace ) ; const length = shardDistribution . length ; let curr = 0 ; let startChunk = curr ; for ( let i = 0 ; i < length ; i ++ ) { for ( startChunk = curr ; shardDistribution [ i ] != null && curr < startChunk + shardDistribution [ i ] ; curr ++ ) { assert . commandWorked ( coll . insert ( { a : curr } ) ) ; } if ( shardDistribution [ i ] == 0 ) { curr ++ ; } if ( shardDistribution [ i ] != null ) { assert . commandWorked ( st . splitAt ( namespace , { a : curr } ) ) ; } assert . commandWorked ( admin . runCommand ( { moveChunk : namespace , find : { a : curr + 1 } , to : shards [ ( i + 1 ) % length ] . _id } ) ) ; } for ( let j = 0 ; shardDistribution [ j ] == null && j < length ; j ++ ) assert . commandWorked ( admin . runCommand ( { moveChunk : namespace , find : { a : curr + 1 } , to : shards [ j + 1 ] . _id } ) ) ; const counts = coll . aggregate ( [ { "$collStats" : { "count" : { } } } ] ) . toArray ( ) ; for ( let i = 0 ; i < shards . length ; i ++ ) { assert . eq ( getShardCount ( counts , shards [ i ] . _id ) [ "count" ] , shardDistribution [ i ] ) ; } st . stop ( ) ; }
Accepts a dbName, collName, and shardDistribution (array of positive integers or nulls).
Accepts a dbName collName and shardDistribution ( array of positive integers or nulls ) .
/* Shard the collection. */ /* Insert shardDistribution[i] documents into the current chunk.*/ /* We need to ensure that we don't split at the same location as we spit previously.  */ /* If the i-th shard is supposed to have documents then split the chunk to the right of
         * where it is supposed to end. Otherwise do not split the chunk. */ /* Move the "next" chunk to the next shard */ /* Move the remaining chunk to the first shard which is supposed to have documents. */