summaryrefslogtreecommitdiff
path: root/jstests/core/hashindex1.js
blob: 61f98f980f3813b609c5f5c2f807009a69131447 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Cannot implicitly shard accessed collections because of extra shard key index in sharded
// collection.
//
// @tags: [assumes_no_implicit_index_creation, requires_fastcount]

var t = db.hashindex1;
t.drop();

// Include helpers for analyzing explain output.
load("jstests/libs/analyze_plan.js");

assert.commandWorked(db.createCollection(t.getName()));

// test non-single field hashed indexes don't get created (maybe change later)
var badspec = {a: "hashed", b: 1};
assert.commandFailedWithCode(t.ensureIndex(badspec), 16763);
assert.eq(t.getIndexes().length, 1, "only _id index should be created");

// test unique index not created (maybe change later)
var goodspec = {a: "hashed"};
assert.commandFailedWithCode(t.ensureIndex(goodspec, {"unique": true}), 16764);
assert.eq(t.getIndexes().length, 1, "unique index got created.");

// now test that non-unique index does get created
assert.commandWorked(t.ensureIndex(goodspec));
assert.eq(t.getIndexes().length, 2, "hashed index didn't get created");

// test basic inserts
for (i = 0; i < 10; i++) {
    assert.commandWorked(t.insert({a: i}));
}
assert.eq(t.find().count(), 10, "basic insert didn't work");
assert.eq(t.find().hint(goodspec).toArray().length, 10, "basic insert didn't work");
assert.eq(t.find({a: 3}).hint({_id: 1}).toArray()[0]._id,
          t.find({a: 3}).hint(goodspec).toArray()[0]._id,
          "hashindex lookup didn't work");

// make sure things with the same hash are not both returned
assert.commandWorked(t.insert({a: 3.1}));
assert.eq(t.find().count(), 11, "additional insert didn't work");
assert.eq(t.find({a: 3.1}).hint(goodspec).toArray().length, 1);
assert.eq(t.find({a: 3}).hint(goodspec).toArray().length, 1);
// test right obj is found
assert.eq(t.find({a: 3.1}).hint(goodspec).toArray()[0].a, 3.1);

// Make sure we're using the hashed index.
var explain = t.find({a: 1}).explain();
assert(isIxscan(db, explain.queryPlanner.winningPlan), "not using hashed index");

// SERVER-12222
// printjson( t.find({a : {$gte : 3 , $lte : 3}}).explain() )
// assert.eq( t.find({a : {$gte : 3 , $lte : 3}}).explain().cursor ,
//		cursorname ,
//		"not using hashed cursor");
var explain = t.find({c: 1}).explain();
assert(!isIxscan(db, explain.queryPlanner.winningPlan), "using irrelevant hashed index");

// Hash index used with a $in set membership predicate.
var explain = t.find({a: {$in: [1, 2]}}).explain();
printjson(explain);
assert(isIxscan(db, explain.queryPlanner.winningPlan), "not using hashed index");

// Hash index used with a singleton $and predicate conjunction.
var explain = t.find({$and: [{a: 1}]}).explain();
assert(isIxscan(db, explain.queryPlanner.winningPlan), "not using hashed index");

// Hash index used with a non singleton $and predicate conjunction.
var explain = t.find({$and: [{a: {$in: [1, 2]}}, {a: {$gt: 1}}]}).explain();
assert(isIxscan(db, explain.queryPlanner.winningPlan), "not using hashed index");

// test creation of index based on hash of _id index
var goodspec2 = {'_id': "hashed"};
assert.commandWorked(t.ensureIndex(goodspec2));
assert.eq(t.getIndexes().length, 3, "_id index didn't get created");

var newid = t.findOne()["_id"];
assert.eq(t.find({_id: newid}).hint({_id: 1}).toArray()[0]._id,
          t.find({_id: newid}).hint(goodspec2).toArray()[0]._id,
          "using hashed index and different index returns different docs");

// test creation of sparse hashed index
var sparseindex = {b: "hashed"};
assert.commandWorked(t.ensureIndex(sparseindex, {"sparse": true}));
assert.eq(t.getIndexes().length, 4, "sparse index didn't get created");

// test sparse index has smaller total items on after inserts
for (i = 0; i < 10; i++) {
    assert.commandWorked(t.insert({b: i}));
}
var totalb = t.find().hint(sparseindex).toArray().length;
assert.eq(totalb, 10, "sparse index has wrong total");

var total = t.find().hint({"_id": 1}).toArray().length;
var totala = t.find().hint(goodspec).toArray().length;
assert.eq(total, totala, "non-sparse index has wrong total");
assert.lt(totalb, totala, "sparse index should have smaller total");

// Test that having arrays along the path of the index is not allowed.
assert.commandWorked(t.createIndex({"field1.field2.0.field4": "hashed"}));
assert.commandFailedWithCode(t.insert({field1: []}), 16766);
assert.commandFailedWithCode(t.insert({field1: {field2: []}}), 16766);
assert.commandFailedWithCode(t.insert({field1: {field2: {0: []}}}), 16766);
assert.commandFailedWithCode(t.insert({field1: [{field2: {0: []}}]}), 16766);
assert.commandFailedWithCode(t.insert({field1: {field2: {0: {field4: []}}}}), 16766);
assert.commandWorked(t.insert({field1: {field2: {0: {otherField: []}}}}));
assert.commandWorked(t.insert({field1: {field2: {0: {field4: 1}}}}));