summaryrefslogtreecommitdiff
path: root/jstests/sharding/sample_direct_connection.js
blob: c435490c2f85de9d6949d38146bf93afe696f766 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// This test ensures that an aggregation pipeline with a $sample stage can be issued through a
// direct connection to a shard without failing, and $sample behaves as if we sampled an unsharded
// collection.
(function() {
let st = new ShardingTest({shards: 1});

assert.commandWorked(st.adminCommand({enableSharding: 'test'}));
assert.commandWorked(st.adminCommand({shardCollection: 'test.sharded', key: {x: 1}}));
const testDB = st.s.getDB('test');

// We must have >100 samples to attempt the storage optimized sample path.
for (let x = 0; x < 101; x++) {
    assert.commandWorked(testDB.foo.insert({x: x}));
}

const shardDB = st.rs0.getPrimary().getDB('test');

const res = assert.commandWorked(
    shardDB.runCommand({aggregate: 'foo', pipeline: [{$sample: {size: 3}}], cursor: {}}));
assert.eq(res.cursor.firstBatch.length, 3);

st.stop();
})();