~ubuntu-branches/ubuntu/wily/juju-mongodb/wily-proposed

1 by James Page
Import upstream version 2.4.8
1
// Test that the shardCollection command fails when a preexisting document lacks a shard key field.
2
// SERVER-8772
3
4
var st = new ShardingTest( { shards: 1 } );
5
st.stopBalancer();
6
7
var db = st.s.getDB( 'testDb' );
8
var coll = db.testColl;
9
10
coll.insert( { x:1, z:1 } );
11
coll.insert( { y:1, z:1 } );
12
db.adminCommand( { enableSharding:'testDb' } );
13
14
/**
15
 * Assert that the shardCollection command fails, with a preexisting index on the provided
16
 * 'shardKey'.
17
 */
18
function assertInvalidShardKey( shardKey ) {
19
20
    // Manually create a shard key index.
21
    coll.dropIndexes();
22
    coll.ensureIndex( shardKey );
23
24
    // Ensure that the shard key index identifies 'x' as present in one document and absent in the
25
    // other.
26
    assert.eq( 1, coll.find( { x:1 } ).hint( shardKey ).itcount() );
27
    assert.eq( 1, coll.find( { x:{ $exists:false } } ).hint( shardKey ).itcount() );
28
29
    // Assert that the shardCollection command fails with the provided 'shardKey'.
30
    assert.commandFailed( db.adminCommand( { shardCollection:'testDb.testColl', key:shardKey } ),
31
                          'shardCollection should have failed on key ' + tojson( shardKey ) );
32
}
33
34
// Test single, compound, and hashed shard keys.
35
assertInvalidShardKey( { x:1 } );
36
assertInvalidShardKey( { x:1, y:1 } );
37
assertInvalidShardKey( { y:1, x:1 } );
38
assertInvalidShardKey( { x:'hashed' } );
39
40
st.stop();