File: hash_shard_unique_compound.js

package info (click to toggle)
mongodb 1%3A3.2.11-2%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 196,404 kB
  • sloc: cpp: 1,412,793; ansic: 504,961; python: 78,522; perl: 5,837; sh: 5,661; java: 4,202; makefile: 1,784; pascal: 617; xml: 176; asm: 128
file content (45 lines) | stat: -rw-r--r-- 1,449 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Basic test of sharding with a hashed shard key and other unique index
// Does 2 things and checks for consistent error:
//  1.) shard collection on hashed "a", ensure unique index {a:1, b:1}
//  2.) reverse order

var s = new ShardingTest({name: jsTestName(), shards: 1, mongos: 1, verbose: 1});
var dbName = "test";
var collName = "foo";
var ns = dbName + "." + collName;
var db = s.getDB(dbName);
var coll = db.getCollection(collName);

// Enable sharding on DB
var res = db.adminCommand({enablesharding: dbName});

// for simplicity start by turning off balancer
var res = s.stopBalancer();

// shard a fresh collection using a hashed shard key
coll.drop();
assert.commandWorked(db.adminCommand({shardcollection: ns, key: {a: "hashed"}}));
db.printShardingStatus();

// Create unique index
assert.commandWorked(coll.ensureIndex({a: 1, b: 1}, {unique: true}));

jsTest.log("------ indexes -------");
jsTest.log(tojson(coll.getIndexes()));

// Second Part
jsTest.log("------ dropping sharded collection to start part 2 -------");
coll.drop();

// Create unique index
assert.commandWorked(coll.ensureIndex({a: 1, b: 1}, {unique: true}));

// shard a fresh collection using a hashed shard key
assert.commandWorked(db.adminCommand({shardcollection: ns, key: {a: "hashed"}}),
                     "shardcollection didn't worked 2");

db.printShardingStatus();
jsTest.log("------ indexes 2-------");
jsTest.log(tojson(coll.getIndexes()));

s.stop();