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
|
// Cannot implicitly shard accessed collections because queries on a sharded collection are not
// able to be covered when they aren't on the shard key since the document needs to be fetched in
// order to apply the SHARDING_FILTER stage.
// @tags: [
// assumes_unsharded_collection,
// ]
// Simple covered index query test with a unique sparse index
// Include helpers for analyzing explain output.
load("jstests/libs/analyze_plan.js");
var coll = db.getCollection("covered_simple_3");
coll.drop();
for (i = 0; i < 10; i++) {
coll.insert({foo: i});
}
for (i = 0; i < 5; i++) {
coll.insert({bar: i});
}
coll.insert({foo: "string"});
coll.insert({foo: {bar: 1}});
coll.insert({foo: null});
coll.createIndex({foo: 1}, {sparse: true, unique: true});
// Test equality with int value
var plan = coll.find({foo: 1}, {foo: 1, _id: 0}).hint({foo: 1}).explain("executionStats");
assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
"simple.3.1 - indexOnly should be true on covered query");
assert.eq(0,
plan.executionStats.totalDocsExamined,
"simple.3.1 - docs examined should be 0 for covered query");
// Test equality with string value
var plan = coll.find({foo: "string"}, {foo: 1, _id: 0}).hint({foo: 1}).explain("executionStats");
assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
"simple.3.2 - indexOnly should be true on covered query");
assert.eq(0,
plan.executionStats.totalDocsExamined,
"simple.3.2 - docs examined should be 0 for covered query");
// Test equality with int value on a dotted field
var plan = coll.find({foo: {bar: 1}}, {foo: 1, _id: 0}).hint({foo: 1}).explain("executionStats");
assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
"simple.3.3 - indexOnly should be true on covered query");
assert.eq(0,
plan.executionStats.totalDocsExamined,
"simple.3.3 - docs examined should be 0 for covered query");
// Test no query
var plan = coll.find({}, {foo: 1, _id: 0}).hint({foo: 1}).explain("executionStats");
assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
"simple.3.4 - indexOnly should be true on covered query");
assert.eq(0,
plan.executionStats.totalDocsExamined,
"simple.3.4 - docs examined should be 0 for covered query");
// Test range query
var plan =
coll.find({foo: {$gt: 2, $lt: 6}}, {foo: 1, _id: 0}).hint({foo: 1}).explain("executionStats");
assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
"simple.3.5 - indexOnly should be true on covered query");
assert.eq(0,
plan.executionStats.totalDocsExamined,
"simple.3.5 - docs examined should be 0 for covered query");
// Test in query
var plan =
coll.find({foo: {$in: [5, 8]}}, {foo: 1, _id: 0}).hint({foo: 1}).explain("executionStats");
assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
"simple.3.6 - indexOnly should be true on covered query");
assert.eq(0,
plan.executionStats.totalDocsExamined,
"simple.3.6 - docs examined should be 0 for covered query");
// Test $exists true
var plan =
coll.find({foo: {$exists: true}}, {foo: 1, _id: 0}).hint({foo: 1}).explain("executionStats");
assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
"simple.3.7 - indexOnly should be true on covered query");
assert.eq(0,
plan.executionStats.totalDocsExamined,
"simple.3.7 - docs examined should be 0 for covered query");
// Check that $nin can be covered.
coll.dropIndexes();
coll.createIndex({bar: 1});
var plan =
coll.find({bar: {$nin: [5, 8]}}, {bar: 1, _id: 0}).hint({bar: 1}).explain("executionStats");
assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
"simple.3.8 - indexOnly should be true on covered query");
assert.eq(0,
plan.executionStats.totalDocsExamined,
"simple.3.8 - docs examined should be 0 for covered query");
print('all tests pass');
|