summaryrefslogtreecommitdiff
path: root/jstests/core/covered_index_negative_1.js
blob: b78357e509d33deb1bed9920a1695a09d5b5f811 (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
// Miscellaneous covered query tests. Mostly negative tests
// These are tests where we do not expect the query to be a
// covered index query. Hence we expect indexOnly=false and
// nscannedObjects > 0

// Include helpers for analyzing explain output.
// @tags: [assumes_balancer_off]
load("jstests/libs/analyze_plan.js");

var coll = db.getCollection("covered_negative_1");
coll.drop();
for (i = 0; i < 100; i++) {
    coll.insert(
        {a: i, b: "strvar_" + (i % 13), c: NumberInt(i % 10), d: i * 10, e: [i, i % 10], f: i});
}
coll.ensureIndex({a: 1, b: -1, c: 1});
coll.ensureIndex({e: 1});
coll.ensureIndex({d: 1});
coll.ensureIndex({f: "hashed"});

// Test no projection
var plan =
    coll.find({a: 10, b: "strvar_10", c: 0}).hint({a: 1, b: -1, c: 1}).explain("executionStats");
assert(!isIndexOnly(db, plan.queryPlanner.winningPlan),
       "negative.1.1 - indexOnly should be false on a non covered query");
assert.neq(0,
           plan.executionStats.totalDocsExamined,
           "negative.1.1 - docs examined should not be 0 for a non covered query");

// Test projection and not excluding _id
var plan = coll.find({a: 10, b: "strvar_10", c: 0}, {a: 1, b: 1, c: 1})
               .hint({a: 1, b: -1, c: 1})
               .explain("executionStats");
assert(!isIndexOnly(db, plan.queryPlanner.winningPlan),
       "negative.1.2 - indexOnly should be false on a non covered query");
assert.neq(0,
           plan.executionStats.totalDocsExamined,
           "negative.1.2 - docs examined should not be 0 for a non covered query");

// Test projection of non-indexed field
var plan = coll.find({d: 100}, {d: 1, c: 1, _id: 0}).hint({d: 1}).explain("executionStats");
assert(!isIndexOnly(db, plan.queryPlanner.winningPlan),
       "negative.1.3 - indexOnly should be false on a non covered query");
assert.neq(0,
           plan.executionStats.totalDocsExamined,
           "negative.1.3 - docs examined should not be 0 for a non covered query");

// Test query and projection on a multi-key index
var plan = coll.find({e: 99}, {e: 1, _id: 0}).hint({e: 1}).explain("executionStats");
assert(!isIndexOnly(db, plan.queryPlanner.winningPlan),
       "negative.1.4 - indexOnly should be false on a non covered query");
assert.neq(0,
           plan.executionStats.totalDocsExamined,
           "negative.1.4 - docs examined should not be 0 for a non covered query");

// Commenting out negative.1.5 and 1.6 pending fix in SERVER-8650
// // Test projection and $natural sort
// var plan = coll.find({a:{$gt:70}},{a:1, b:1, c:1, _id:0}).sort({$natural:1}).hint({a:1, b:-1,
// c:1}).explain()
// // indexOnly should be false but is not due to bug https://jira.mongodb.org/browse/SERVER-8561
// assert.eq(true, plan.indexOnly, "negative.1.5 - indexOnly should be false on a non covered
// query")
// assert.neq(0, plan.nscannedObjects, "negative.1.5 - nscannedObjects should not be 0 for a non
// covered query")

// // Test sort on non-indexed field
// var plan = coll.find({d:{$lt:1000}},{d:1, _id:0}).sort({c:1}).hint({d:1}).explain()
// //indexOnly should be false but is not due to bug https://jira.mongodb.org/browse/SERVER-8562
// assert.eq(true, plan.indexOnly, "negative.1.6 - indexOnly should be false on a non covered
// query")
// assert.neq(0, plan.nscannedObjects, "negative.1.6 - nscannedObjects should not be 0 for a non
// covered query")

// Test query on non-indexed field
var plan = coll.find({d: {$lt: 1000}}, {a: 1, b: 1, c: 1, _id: 0})
               .hint({a: 1, b: -1, c: 1})
               .explain("executionStats");
assert(!isIndexOnly(db, plan.queryPlanner.winningPlan),
       "negative.1.7 - indexOnly should be false on a non covered query");
assert.neq(0,
           plan.executionStats.totalDocsExamined,
           "negative.1.7 - docs examined should not be 0 for a non covered query");

// Test query on hashed indexed field
var plan = coll.find({f: 10}, {f: 1, _id: 0}).hint({f: "hashed"}).explain("executionStats");
assert(!isIndexOnly(db, plan.queryPlanner.winningPlan),
       "negative.1.8 - indexOnly should be false on a non covered query");
assert.neq(0,
           plan.executionStats.totalDocsExamined,
           "negative.1.8 - nscannedObjects should not be 0 for a non covered query");

print('all tests passed');