summaryrefslogtreecommitdiff
path: root/jstests/aggregation/use_query_project_and_sort.js
blob: dbe5b6a9f6f92ad84de4020c13e6904ba87c3689 (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
// Tests that an aggregation that only needs a finite set of fields to do its computations, and has
// a $sort stage near the front of the pipeline can use the query system to provide a covered plan
// which only returns those fields, in the desired order, without fetching the full document.
//
// Relies on the ability to push leading $sorts down to the query system, so cannot wrap pipelines
// in $facet stages:
// @tags: [
//   do_not_wrap_aggregations_in_facets,
// ]
(function() {
"use strict";

load("jstests/libs/analyze_plan.js");  // For 'aggPlanHasStage' and other explain helpers.

const coll = db.use_query_project_and_sort;
coll.drop();

const bulk = coll.initializeUnorderedBulkOp();
for (let i = 0; i < 100; ++i) {
    bulk.insert({_id: i, x: "string", a: -i, y: i % 2});
}
assert.commandWorked(bulk.execute());

function assertQueryCoversProjectionAndSort(pipeline) {
    const explainOutput = coll.explain().aggregate(pipeline);
    assert(isQueryPlan(explainOutput), explainOutput);
    assert(!planHasStage(db, explainOutput, "FETCH"),
           "Expected pipeline " + tojsononeline(pipeline) +
               " *not* to include a FETCH stage in the explain output: " + tojson(explainOutput));
    assert(!planHasStage(db, explainOutput, "SORT"),
           "Expected pipeline " + tojsononeline(pipeline) +
               " *not* to include a SORT stage in the explain output: " + tojson(explainOutput));
    assert(planHasStage(db, explainOutput, "IXSCAN"),
           "Expected pipeline " + tojsononeline(pipeline) +
               " to include an index scan in the explain output: " + tojson(explainOutput));
    assert(!hasRejectedPlans(explainOutput),
           "Expected pipeline " + tojsononeline(pipeline) +
               " not to have any rejected plans in the explain output: " + tojson(explainOutput));
    return explainOutput;
}

assert.commandWorked(coll.createIndex({x: 1, a: -1, _id: 1}));

// Test that a pipeline requiring a subset of the fields in a compound index can use that index
// to cover the query.
assertQueryCoversProjectionAndSort(
    [{$match: {x: "string"}}, {$sort: {x: 1}}, {$project: {_id: 0, x: 1}}]);
assertQueryCoversProjectionAndSort(
    [{$match: {x: "string"}}, {$sort: {x: 1}}, {$project: {_id: 1, x: 1}}]);
assertQueryCoversProjectionAndSort(
    [{$match: {x: "string"}}, {$sort: {x: -1, a: 1}}, {$project: {_id: 1, x: 1}}]);
assertQueryCoversProjectionAndSort(
    [{$match: {x: "string"}}, {$sort: {x: 1, a: -1, _id: 1}}, {$project: {_id: 1}}]);
assertQueryCoversProjectionAndSort(
    [{$match: {x: "string"}}, {$sort: {x: 1, a: -1, _id: 1}}, {$project: {_id: 1, x: 1}}]);
assertQueryCoversProjectionAndSort(
    [{$match: {x: "string"}}, {$sort: {x: 1, a: -1, _id: 1}}, {$project: {_id: 1, a: 1}}]);
assertQueryCoversProjectionAndSort(
    [{$match: {x: "string"}}, {$sort: {x: 1, a: -1, _id: 1}}, {$project: {_id: 0, a: 1, x: 1}}]);
assertQueryCoversProjectionAndSort(
    [{$match: {x: "string"}}, {$sort: {x: 1, a: -1, _id: 1}}, {$project: {_id: 1, x: 1, a: 1}}]);
}());