summaryrefslogtreecommitdiff
path: root/jstests/core/index/index8.js
blob: 3887906dddc2a3afd9e3a1b284199474c4719595 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// The test runs commands that are not allowed with security token: reIndex.
// @tags: [
//   not_allowed_with_security_token,
//     # Asserts on the output of listIndexes.
//     assumes_no_implicit_index_creation,
//     # Cannot implicitly shard accessed collections because of not being able to create unique
//     # index using hashed shard key pattern.
//     cannot_create_unique_index_when_using_hashed_shard_key,
//     requires_fastcount
// ]

// Test key uniqueness
(function() {

t = db.jstests_index8;
t.drop();

t.createIndex({a: 1});
t.createIndex({b: 1}, true);
t.createIndex({c: 1}, [false, "cIndex"]);

checkIndexes = function(num) {
    const indexes = t.getIndexes();
    assert.eq(4, indexes.length);

    let start = 0;
    if (indexes[0].name == "_id_")
        start = 1;
    assert(!indexes[start].unique, "A" + num);
    assert(indexes[start + 1].unique, "B" + num + " " + tojson(indexes[start + 1]));
    assert(!indexes[start + 2].unique, "C" + num);
    assert.eq("cIndex", indexes[start + 2].name, "D" + num);
};

checkIndexes(1);

// The reIndex command is only supported in standalone mode.
const hello = db.runCommand({hello: 1});
const isStandalone = hello.msg !== "isdbgrid" && !hello.hasOwnProperty('setName');
if (isStandalone) {
    assert.commandWorked(t.reIndex());
    checkIndexes(2);
}

t.save({a: 2, b: 1});
t.save({a: 2});
assert.eq(2, t.find().count());

t.save({b: 4});
t.save({b: 4});
assert.eq(3, t.find().count());
assert.eq(3, t.find().hint({c: 1}).toArray().length);
assert.eq(3, t.find().hint({b: 1}).toArray().length);
assert.eq(3, t.find().hint({a: 1}).toArray().length);

t.drop();
t.createIndex({a: 1, b: -1}, true);
t.save({a: 2, b: 3});
t.save({a: 2, b: 3});
t.save({a: 2, b: 4});
t.save({a: 1, b: 3});
assert.eq(3, t.find().count());

t.drop();
t.createIndex({a: 1}, true);
t.save({a: [2, 3]});
t.save({a: 2});
assert.eq(1, t.find().count());

t.drop();
t.createIndex({a: 1}, true);
t.save({a: 2});
t.save({a: [1, 2, 3]});
t.save({a: [3, 2, 1]});
assert.eq(1, t.find().sort({a: 1}).hint({a: 1}).toArray().length);
assert.eq(1, t.find().sort({a: -1}).hint({a: 1}).toArray().length);

assert.eq(t._indexSpec({x: 1}, true), t._indexSpec({x: 1}, [true]), "spec 1");
assert.eq(t._indexSpec({x: 1}, "eliot"), t._indexSpec({x: 1}, ["eliot"]), "spec 2");
})();