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

load("jstests/libs/optimizer_utils.js");  // For assertValueOnPlanPath.

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);
    assertValueOnPlanPath("IndexScan", res, "child.leftChild.child.nodeType");
}
{
    const res =
        t.explain("executionStats").aggregate([{$match: {a: {$elemMatch: {$lt: 11, $gt: 9}}}}]);
    assert.eq(1, res.executionStats.nReturned);
    assertValueOnPlanPath("IndexScan", res, "child.leftChild.child.nodeType");
}
{
    // Contradiction.
    const res =
        t.explain("executionStats").aggregate([{$match: {a: {$elemMatch: {$lt: 5, $gt: 6}}}}]);
    assert.eq(0, res.executionStats.nReturned);
    assertValueOnPlanPath("CoScan", res, "child.child.child.nodeType");
}
}());