summaryrefslogtreecommitdiff
path: root/jstests/core/covered_index_sort_1.js
blob: 1a7ac940efa7de3b6d6fd0a00dbef2af91b4dce3 (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
// 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 sort

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

var coll = db.getCollection("covered_sort_1");
coll.drop();
for (i = 0; i < 10; i++) {
    coll.insert({foo: i});
}
for (i = 0; i < 10; i++) {
    coll.insert({foo: i});
}
for (i = 0; i < 5; i++) {
    coll.insert({bar: i});
}
coll.insert({foo: "1"});
coll.insert({foo: {bar: 1}});
coll.insert({foo: null});
coll.ensureIndex({foo: 1});

// Test no query and sort ascending
var plan = coll.find({}, {foo: 1, _id: 0}).sort({foo: 1}).hint({foo: 1}).explain("executionStats");
assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
       "sort.1.1 - indexOnly should be true on covered query");
assert.eq(0,
          plan.executionStats.totalDocsExamined,
          "sort.1.1 - docs examined should be 0 for covered query");

// Test no query and sort descending
var plan = coll.find({}, {foo: 1, _id: 0}).sort({foo: -1}).hint({foo: 1}).explain("executionStats");
assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
       "sort.1.2 - indexOnly should be true on covered query");
assert.eq(0,
          plan.executionStats.totalDocsExamined,
          "sort.1.2 - docs examined should be 0 for covered query");

// Test range query with sort
var plan = coll.find({foo: {$gt: 2}}, {foo: 1, _id: 0})
               .sort({foo: -1})
               .hint({foo: 1})
               .explain("executionStats");
assert(isIndexOnly(db, plan.queryPlanner.winningPlan),
       "sort.1.3 - indexOnly should be true on covered query");
assert.eq(0,
          plan.executionStats.totalDocsExamined,
          "sort.1.3 - docs examined should be 0 for covered query");

print('all tests pass');