summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod/wildcard_index_minmax.js
blob: 85b4eca86d02efe3db4cbfb7d364062d65a90815 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
 * Tests that min/max is not supported for wildcard index.
 */
(function() {
    "use strict";

    load("jstests/aggregation/extras/utils.js");  // For arrayEq.

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

    const assertArrayEq = (l, r) => assert(arrayEq(l, r), tojson(l) + " != " + tojson(r));

    assert.commandWorked(coll.createIndex({"$**": 1}));

    assert.commandWorked(coll.insert({a: 1, b: 1}));
    assert.commandWorked(coll.insert({a: 1, b: 2}));
    assert.commandWorked(coll.insert({a: 2, b: 1}));
    assert.commandWorked(coll.insert({a: 2, b: 2}));

    // Throws error for $** index min.
    assert.commandFailedWithCode(
        db.runCommand({find: coll.getName(), min: {"a": 0.5}, hint: {"$**": 1}}),
        ErrorCodes.BadValue);

    // Throws error for $** index max.
    assert.commandFailedWithCode(
        db.runCommand({find: coll.getName(), max: {"a": 1.5}, hint: {"$**": 1}}),
        ErrorCodes.BadValue);

    // Throws error for $** index min/max.
    assert.commandFailedWithCode(
        db.runCommand({find: coll.getName(), min: {"a": 0.5}, max: {"a": 1.5}, hint: {"$**": 1}}),
        ErrorCodes.BadValue);

    // Throws error for $** index min with filter of a different value.
    assert.commandFailedWithCode(
        db.runCommand({find: coll.getName(), filter: {"a": 2}, min: {"a": 1}, hint: {"$**": 1}}),
        ErrorCodes.BadValue);

    // Throws error for $** index max with filter of a different value.
    assert.commandFailedWithCode(
        db.runCommand({find: coll.getName(), filter: {"a": 1}, max: {"a": 1.5}, hint: {"$**": 1}}),
        ErrorCodes.BadValue);

    // Throws error for $** index min and max with filter of a different value.
    assert.commandFailedWithCode(db.runCommand({
        find: coll.getName(),
        filter: {"a": 1},
        min: {"a": 0.5},
        max: {"a": 1.5},
        hint: {"$**": 1}
    }),
                                 ErrorCodes.BadValue);

    // Throws error for $** index min with filter of the same value.
    assert.commandFailedWithCode(
        db.runCommand({find: coll.getName(), filter: {"a": 1}, min: {"a": 1}, hint: {"$**": 1}}),
        ErrorCodes.BadValue);

    // Throws error for $** index max with filter of the same value.
    assert.commandFailedWithCode(
        db.runCommand({find: coll.getName(), filter: {"a": 1}, max: {"a": 1}, hint: {"$**": 1}}),
        ErrorCodes.BadValue);

    // Throws error for $** index min and max with filter of the same value.
    assert.commandFailedWithCode(db.runCommand({
        find: coll.getName(),
        filter: {"a": 1},
        min: {"a": 1},
        max: {"a": 1},
        hint: {"$**": 1}
    }),
                                 ErrorCodes.BadValue);

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

    // $** index does not interfere with valid min/max.
    assertArrayEq(coll.find({}, {_id: 0}).min({"a": 0.5}).max({"a": 1.5}).toArray(),
                  [{a: 1, b: 1}, {a: 1, b: 2}]);
})();