summaryrefslogtreecommitdiff
path: root/jstests/core/wildcard_index_partial_index.js
blob: 5961caea87a0e4f8bf0a770865a0da9ca2854b0e (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
/**
 * Test that $** indexes work when provided with a partial filter expression.
 */
(function() {
    "use strict";

    load("jstests/libs/analyze_plan.js");  // For isIxScan, isCollscan.

    const coll = db.wildcard_partial_index;

    function testPartialWildcardIndex(indexKeyPattern, indexOptions) {
        coll.drop();

        assert.commandWorked(coll.createIndex(indexKeyPattern, indexOptions));
        assert.commandWorked(coll.insert({x: 5, a: 2}));  // Not in index.
        assert.commandWorked(coll.insert({x: 6, a: 1}));  // In index.

        // find() operations that should use the index.
        let explain = coll.explain("executionStats").find({x: 6, a: 1}).finish();
        assert.eq(1, explain.executionStats.nReturned);
        assert(isIxscan(db, explain.queryPlanner.winningPlan));
        explain = coll.explain("executionStats").find({x: {$gt: 1}, a: 1}).finish();
        assert.eq(1, explain.executionStats.nReturned);
        assert(isIxscan(db, explain.queryPlanner.winningPlan));
        explain = coll.explain("executionStats").find({x: 6, a: {$lte: 1}}).finish();
        assert.eq(1, explain.executionStats.nReturned);
        assert(isIxscan(db, explain.queryPlanner.winningPlan));

        // find() operations that should not use the index.
        explain = coll.explain("executionStats").find({x: 6, a: {$lt: 1.6}}).finish();
        assert.eq(1, explain.executionStats.nReturned);
        assert(isCollscan(db, explain.queryPlanner.winningPlan));

        explain = coll.explain("executionStats").find({x: 6}).finish();
        assert.eq(1, explain.executionStats.nReturned);
        assert(isCollscan(db, explain.queryPlanner.winningPlan));

        explain = coll.explain("executionStats").find({a: {$gte: 0}}).finish();
        assert.eq(2, explain.executionStats.nReturned);
        assert(isCollscan(db, explain.queryPlanner.winningPlan));
    }

    // Case where the partial filter expression is on a field in the index.
    testPartialWildcardIndex({"$**": 1}, {partialFilterExpression: {a: {$lte: 1.5}}});

    // Case where the partial filter expression is on a field not included in the index.
    testPartialWildcardIndex({"x.$**": 1}, {partialFilterExpression: {a: {$lte: 1.5}}});
})();