forked from mongodb/mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrop_index.js
74 lines (63 loc) · 2.91 KB
/
drop_index.js
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Cannot implicitly shard accessed collections because of extra shard key index in sharded
// collection.
// @tags: [assumes_no_implicit_index_creation]
import {FixtureHelpers} from "jstests/libs/fixture_helpers.js";
const t = db.drop_index;
t.drop();
/**
* Extracts index names from listIndexes result.
*/
function getIndexNames(cmdRes) {
return t.getIndexes().map(spec => spec.name);
}
/**
* Checks that collection contains the given list of non-id indexes and nothing else.
*/
function assertIndexes(expectedIndexNames, msg) {
const actualIndexNames = getIndexNames();
const testMsgSuffix = () => msg + ': expected ' + tojson(expectedIndexNames) + ' but got ' +
tojson(actualIndexNames) + ' instead.';
assert.eq(expectedIndexNames.length + 1,
actualIndexNames.length,
'unexpected number of indexes after ' + testMsgSuffix());
assert(actualIndexNames.includes('_id_'),
'_id index missing after ' + msg + ': ' + tojson(actualIndexNames));
for (let expectedIndexName of expectedIndexNames) {
assert(actualIndexNames.includes(expectedIndexName),
expectedIndexName + ' index missing after ' + testMsgSuffix());
}
}
assert.commandWorked(t.insert({_id: 1, a: 2, b: 3, c: 1, d: 1, e: 1}));
assertIndexes([], 'inserting test document');
assert.commandWorked(t.createIndex({a: 1}));
assert.commandWorked(t.createIndex({b: 1}));
assert.commandWorked(t.createIndex({c: 1}));
assert.commandWorked(t.createIndex({d: 1}));
assert.commandWorked(t.createIndex({e: 1}));
assertIndexes(['a_1', 'b_1', 'c_1', 'd_1', 'e_1'], 'creating indexes');
// Drop single index by name.
// Collection.dropIndex() throws if the dropIndexes command fails.
assert.commandWorked(t.dropIndex(t._genIndexName({a: 1})));
assertIndexes(['b_1', 'c_1', 'd_1', 'e_1'], 'dropping {a: 1} by name');
// Drop single index by key pattern.
assert.commandWorked(t.dropIndex({b: 1}));
assertIndexes(['c_1', 'd_1', 'e_1'], 'dropping {b: 1} by key pattern');
const runningOnMongos = FixtureHelpers.isMongos(db);
// Not allowed to drop _id index.
for (const dropIndexArg of ['_id_', {_id: 1}]) {
const dropIdIndexReply = t.dropIndex(dropIndexArg);
jsTestLog(`Reply to dropIndexes with arg ${tojson(dropIndexArg)}: ${tojson(dropIdIndexReply)}`);
assert.commandFailedWithCode(dropIdIndexReply, ErrorCodes.InvalidOptions);
assert(dropIdIndexReply.hasOwnProperty('errmsg'));
if (runningOnMongos) {
assert(dropIdIndexReply.hasOwnProperty('raw'));
}
}
// Ensure you can recreate indexes, even if you don't use dropIndex method.
// Prior to SERVER-7168, the shell used to cache names of indexes created using
// Collection.createIndex().
assert.commandWorked(t.createIndex({a: 1}));
assertIndexes(['a_1', 'c_1', 'd_1', 'e_1'], 'recreating {a: 1}');
// Drop single index with dropIndexes().
assert.commandWorked(t.dropIndexes(['c_1']));
assertIndexes(['a_1', 'd_1', 'e_1'], 'dropping {c: 1}');