summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod/projection_optimizations.js
blob: 8658d6f226043cce6ed37d24c2083884dc990fec (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
/**
 * Test projections with $and in cases where optimizations could be performed.
 */
(function() {
"use strict";

load("jstests/aggregation/extras/utils.js");
load('jstests/libs/analyze_plan.js');

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

assert.commandWorked(coll.insert({_id: 0, a: 1}));
assert.commandWorked(coll.createIndex({a: 1}));

function runFindWithProjection({filter = {}, projection, expected} = {}) {
    const res = coll.find(filter, projection);
    assertArrayEq({actual: res.toArray(), expected: expected});
    return res;
}

let result = runFindWithProjection({
    filter: {a: 1},
    projection: {_id: 0, a: 1, b: {$and: [false, "$b"]}},
    expected: [{a: 1, b: false}]
});
// Query should be optimized and covered.
assert(isIndexOnly(db, getWinningPlan(result.explain().queryPlanner)));

result = runFindWithProjection(
    {projection: {a: {$and: ['$a', true, 1]}}, expected: [{_id: 0, a: true}]});
assert(isCollscan(db, getWinningPlan(result.explain().queryPlanner)));
})();