summaryrefslogtreecommitdiff
path: root/jstests/cqf/value_elemMatch.js
blob: 4bb46e6f1a78bf94588e123892eeff4cf2b4923b (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
(function() {
"use strict";

load("jstests/libs/optimizer_utils.js");  // For checkCascadesOptimizerEnabled.
if (!checkCascadesOptimizerEnabled(db)) {
    jsTestLog("Skipping test because the optimizer is not enabled");
    return;
}

const t = db.cqf_value_elemMatch;
t.drop();

assert.commandWorked(t.insert({a: [1, 2, 3, 4, 5, 6]}));
assert.commandWorked(t.insert({a: [5, 6, 7, 8, 9]}));
assert.commandWorked(t.insert({a: [1, 2, 3]}));
assert.commandWorked(t.insert({a: []}));
assert.commandWorked(t.insert({a: [1]}));
assert.commandWorked(t.insert({a: [10]}));
assert.commandWorked(t.insert({a: 5}));
assert.commandWorked(t.insert({a: 6}));

// Generate enough documents for index to be preferable.
const nDocs = 400;
for (let i = 0; i < nDocs; i++) {
    assert.commandWorked(t.insert({a: i + 10}));
}

assert.commandWorked(t.createIndex({a: 1}));

{
    // Value elemMatch. Demonstrate we can use an index.
    const res =
        t.explain("executionStats").aggregate([{$match: {a: {$elemMatch: {$gte: 5, $lte: 6}}}}]);
    assert.eq(2, res.executionStats.nReturned);
    assert.eq("IndexScan",
              res.queryPlanner.winningPlan.optimizerPlan.child.child.leftChild.child.nodeType);
}
{
    const res =
        t.explain("executionStats").aggregate([{$match: {a: {$elemMatch: {$lt: 11, $gt: 9}}}}]);
    assert.eq(1, res.executionStats.nReturned);
    assert.eq("IndexScan",
              res.queryPlanner.winningPlan.optimizerPlan.child.child.leftChild.child.nodeType);
}
{
    // Contradiction.
    const res =
        t.explain("executionStats").aggregate([{$match: {a: {$elemMatch: {$lt: 5, $gt: 6}}}}]);
    assert.eq(0, res.executionStats.nReturned);
    assert.eq("CoScan", res.queryPlanner.winningPlan.optimizerPlan.child.child.child.nodeType);
}
}());