summaryrefslogtreecommitdiff
path: root/jstests/core/index_plugins.js
blob: e535d189e6c6a323b1ff1ca4e9fd4855ae33a38e (plain)
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
// Test creation of compound indexes with special index types.
(function() {
"use strict";
const coll = db.index_plugins;
coll.drop();

// Test building special index types on a single field.

assert.commandWorked(coll.createIndex({a: "hashed"}));
coll.dropIndexes();
assert.commandWorked(coll.createIndex({a: "2d"}));
coll.dropIndexes();
assert.commandWorked(coll.createIndex({a: "2dsphere"}));
coll.dropIndexes();
assert.commandWorked(coll.createIndex({a: "text"}));
coll.dropIndexes();

// Test compounding special index types with an ascending index.

assert.commandWorked(coll.createIndex({a: "2dsphere", b: 1}));
coll.dropIndexes();
assert.commandWorked(coll.createIndex({a: 1, b: "2dsphere"}));
coll.dropIndexes();

assert.commandWorked(coll.createIndex({a: "text", b: 1}));
coll.dropIndexes();
assert.commandWorked(coll.createIndex({a: 1, b: "text"}));
coll.dropIndexes();

assert.commandWorked(coll.createIndex({a: "2d", b: 1}));
assert.commandWorked(coll.createIndex({a: "hashed", b: 1}));
assert.commandWorked(coll.createIndex({a: 1, b: "hashed"}));

coll.dropIndexes();
assert.commandFailed(coll.createIndex({a: 1, b: "2d"}));  // unsupported

// Test compound index where multiple fields have same special index type.
coll.dropIndexes();
assert.commandWorked(coll.createIndex({a: "2dsphere", b: "2dsphere"}));
assert.commandWorked(coll.createIndex({a: "text", b: "text"}));
// Unsupported.
assert.commandFailed(coll.createIndex({a: "2d", b: "2d"}));
assert.commandFailedWithCode(coll.createIndex({a: "hashed", b: "hashed"}), 31303);
assert.commandFailedWithCode(coll.createIndex({c: 1, a: "hashed", b: "hashed"}), 31303);

// Test compounding different special index types with each other.
const incompatableIndexTypes = ["2d", "2dsphere", "hashed", "text"];
for (let indexType1 of incompatableIndexTypes) {
    for (let indexType2 of incompatableIndexTypes) {
        if (indexType1 == indexType2) {
            continue;
        }
        assert.commandFailedWithCode(coll.createIndex({a: indexType1, b: indexType2}),
                                     ErrorCodes.CannotCreateIndex);
        assert.commandFailedWithCode(coll.createIndex({a: indexType1, b: indexType2, c: 1}),
                                     ErrorCodes.CannotCreateIndex);
        assert.commandFailedWithCode(coll.createIndex({c: -1, a: indexType1, b: indexType2}),
                                     ErrorCodes.CannotCreateIndex);
        assert.commandFailedWithCode(coll.createIndex({a: indexType1, c: 1, b: indexType2}),
                                     ErrorCodes.CannotCreateIndex);
    }
    assert.commandFailedWithCode(coll.createIndex({"$**": 1, b: indexType1}),
                                 ErrorCodes.CannotCreateIndex);
}
})();