1
// Simple covered index query test with a unique sparse index
3
var coll = db.getCollection("covered_simple_3")
11
coll.insert({foo:"string"})
12
coll.insert({foo:{bar:1}})
13
coll.insert({foo:null})
14
coll.ensureIndex({foo:1}, {sparse:true, unique:true})
16
// Test equality with int value
17
var plan = coll.find({foo:1}, {foo:1, _id:0}).hint({foo:1}).explain()
18
assert.eq(true, plan.indexOnly, "simple.3.1 - indexOnly should be true on covered query")
19
assert.eq(0, plan.nscannedObjects, "simple.3.1 - nscannedObjects should be 0 for covered query")
21
// Test equality with string value
22
var plan = coll.find({foo:"string"}, {foo:1, _id:0}).hint({foo:1}).explain()
23
assert.eq(true, plan.indexOnly, "simple.3.2 - indexOnly should be true on covered query")
24
assert.eq(0, plan.nscannedObjects, "simple.3.2 - nscannedObjects should be 0 for covered query")
26
// Test equality with int value on a dotted field
27
var plan = coll.find({foo:{bar:1}}, {foo:1, _id:0}).hint({foo:1}).explain()
28
assert.eq(true, plan.indexOnly, "simple.3.3 - indexOnly should be true on covered query")
29
assert.eq(0, plan.nscannedObjects, "simple.3.3 - nscannedObjects should be 0 for covered query")
32
var plan = coll.find({}, {foo:1, _id:0}).hint({foo:1}).explain()
33
assert.eq(true, plan.indexOnly, "simple.3.4 - indexOnly should be true on covered query")
34
assert.eq(0, plan.nscannedObjects, "simple.3.4 - nscannedObjects should be 0 for covered query")
37
var plan = coll.find({foo:{$gt:2,$lt:6}}, {foo:1, _id:0}).hint({foo:1}).explain()
38
assert.eq(true, plan.indexOnly, "simple.3.5 - indexOnly should be true on covered query")
39
assert.eq(0, plan.nscannedObjects, "simple.3.5 - nscannedObjects should be 0 for covered query")
42
var plan = coll.find({foo:{$in:[5,8]}}, {foo:1, _id:0}).hint({foo:1}).explain()
43
assert.eq(true, plan.indexOnly, "simple.3.6 - indexOnly should be true on covered query")
44
assert.eq(0, plan.nscannedObjects, "simple.3.6 - nscannedObjects should be 0 for covered query")
47
var plan = coll.find({foo:{$exists:true}}, {foo:1, _id:0}).hint({foo:1}).explain()
48
assert.eq(true, plan.indexOnly, "simple.3.7 - indexOnly should be true on covered query")
49
// this should be 0 but is not due to bug https://jira.mongodb.org/browse/SERVER-3187
50
assert.eq(13, plan.nscannedObjects, "simple.3.7 - nscannedObjects should be 0 for covered query")
53
var plan = coll.find({foo:{$nin:[5,8]}}, {foo:1, _id:0}).hint({foo:1}).explain()
54
assert.eq(true, plan.indexOnly, "simple.3.8 - indexOnly should be true on covered query")
55
// this should be 0 but is not due to bug https://jira.mongodb.org/browse/SERVER-3187
56
assert.eq(13, plan.nscannedObjects, "simple.3.8 - nscannedObjects should be 0 for covered query")
58
print ('all tests pass')
b'\\ No newline at end of file'