summaryrefslogtreecommitdiff
path: root/jstests/core/min_max_key.js
blob: d65d68292fa594b59f38aa89c063d78239231eee (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Tests the behavior of queries using MinKey and MaxKey

(function() {
    "use strict";

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

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

    const allElements = [
        {_id: "a_max_key", a: MaxKey},
        {_id: "a_min_key", a: MinKey},
        {_id: "a_null", a: null},
        {_id: "a_number", a: 4},
        {_id: "a_subobject", a: {b: "hi"}},
        {_id: "a_undefined", a: undefined},
        {_id: "a_string", a: "hello"}
    ];

    assert.writeOK(coll.insert(allElements));

    function testQueriesWithMinOrMaxKey() {
        const eqMinRes = coll.find({a: {$eq: MinKey}}).toArray();
        const expectedEqMin = [{_id: "a_min_key", a: MinKey}];
        assert(resultsEq(expectedEqMin, eqMinRes), tojson(eqMinRes));

        const gtMinRes = coll.find({a: {$gt: MinKey}}).toArray();
        const expectedGtMin = [
            {_id: "a_max_key", a: MaxKey},
            {_id: "a_null", a: null},
            {_id: "a_number", a: 4},
            {_id: "a_subobject", a: {b: "hi"}},
            {_id: "a_undefined", a: undefined},
            {_id: "a_string", a: "hello"}
        ];
        assert(resultsEq(expectedGtMin, gtMinRes), tojson(gtMinRes));

        const gteMinRes = coll.find({a: {$gte: MinKey}}).toArray();
        assert(resultsEq(allElements, gteMinRes), tojson(gteMinRes));

        const ltMinRes = coll.find({a: {$lt: MinKey}}).toArray();
        assert(resultsEq([], ltMinRes), tojson(ltMinRes));

        const lteMinRes = coll.find({a: {$lte: MinKey}}).toArray();
        assert(resultsEq(expectedEqMin, lteMinRes), tojson(lteMinRes));

        const eqMaxRes = coll.find({a: {$eq: MaxKey}}).toArray();
        const expectedEqMax = [{_id: "a_max_key", a: MaxKey}];
        assert(resultsEq(expectedEqMax, eqMaxRes), tojson(eqMaxRes));

        const gtMaxRes = coll.find({a: {$gt: MaxKey}}).toArray();
        assert(resultsEq([], gtMaxRes), tojson(gtMaxRes));

        const gteMaxRes = coll.find({a: {$gte: MaxKey}}).toArray();
        assert(resultsEq(expectedEqMax, gteMaxRes), tojson(gteMaxRes));

        const ltMaxRes = coll.find({a: {$lt: MaxKey}}).toArray();
        const expectedLtMax = [
            {_id: "a_min_key", a: MinKey},
            {_id: "a_null", a: null},
            {_id: "a_number", a: 4},
            {_id: "a_subobject", a: {b: "hi"}},
            {_id: "a_undefined", a: undefined},
            {_id: "a_string", a: "hello"}
        ];
        assert(resultsEq(expectedLtMax, ltMaxRes), tojson(ltMaxRes));

        const lteMaxRes = coll.find({a: {$lte: MaxKey}}).toArray();
        assert(resultsEq(allElements, lteMaxRes), tojson(lteMaxRes));
    }

    function testTypeBracketedQueries() {
        // Queries that do not involve MinKey or MaxKey follow type bracketing and thus do not
        // return MinKey or MaxKey as results. These queries are being run to test this
        // functionality.
        const numRes = coll.find({a: {$gt: 3}}).toArray();
        const expectedNum = [{_id: "a_number", a: 4}];
        assert(resultsEq(expectedNum, numRes), tojson(numRes));
        const noNum = coll.find({a: {$lt: 3}}).toArray();
        assert(resultsEq([], noNum), tojson(noNum));

        const stringRes = coll.find({a: {$gt: "best"}}).toArray();
        const expectedString = [{_id: "a_string", a: "hello"}];
        assert(resultsEq(expectedString, stringRes), tojson(stringRes));
    }

    testQueriesWithMinOrMaxKey();
    testTypeBracketedQueries();

    assert.commandWorked(coll.createIndex({a: 1}));
    // TODO: SERVER-35921 The results of the queries above should not change based on the
    // presence of an index
    assert.commandWorked(coll.dropIndexes());

    testQueriesWithMinOrMaxKey();
    testTypeBracketedQueries();
}());