summaryrefslogtreecommitdiff
path: root/jstests/core/mod.js
blob: bf68927f90542c691e66ef7a209fb4dea277ee04 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Tests the behavior of $mod for match expressions.

(function() {
    "use strict";

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

    function assertDocumentsFromMod(divider, remainder, expectedDocuments) {
        const actualDocuments =
            coll.find({a: {$mod: [divider, remainder]}}).sort({_id: 1}).toArray();
        assert.eq(expectedDocuments, actualDocuments);
    }

    // Check mod with divisor and remainder which do not fit into int type.
    assert.writeOK(coll.insert([
        {_id: 1, a: 4000000000},
        {_id: 2, a: 15000000000},
        {_id: 3, a: -4000000000},
        {_id: 4, a: -15000000000},
        {_id: 5, a: 4000000000.12345},
        {_id: 6, a: 15000000000.12345},
        {_id: 7, a: -4000000000.12345},
        {_id: 8, a: -15000000000.12345},
        {_id: 9, a: NumberDecimal(4000000000.12345)},
        {_id: 10, a: NumberDecimal(15000000000.12345)},
        {_id: 11, a: NumberDecimal(-4000000000.12345)},
        {_id: 12, a: NumberDecimal(-15000000000.12345)},
    ]));

    assertDocumentsFromMod(4000000000, 0, [
        {_id: 1, a: 4000000000},
        {_id: 3, a: -4000000000},
        {_id: 5, a: 4000000000.12345},
        {_id: 7, a: -4000000000.12345},
        {_id: 9, a: NumberDecimal(4000000000.12345)},
        {_id: 11, a: NumberDecimal(-4000000000.12345)},
    ]);

    assertDocumentsFromMod(-4000000000, 0, [
        {_id: 1, a: 4000000000},
        {_id: 3, a: -4000000000},
        {_id: 5, a: 4000000000.12345},
        {_id: 7, a: -4000000000.12345},
        {_id: 9, a: NumberDecimal(4000000000.12345)},
        {_id: 11, a: NumberDecimal(-4000000000.12345)},
    ]);

    assertDocumentsFromMod(10000000000, 5000000000, [
        {_id: 2, a: 15000000000},
        {_id: 6, a: 15000000000.12345},
        {_id: 10, a: NumberDecimal(15000000000.12345)},
    ]);

    assertDocumentsFromMod(10000000000, -5000000000, [
        {_id: 4, a: -15000000000},
        {_id: 8, a: -15000000000.12345},
        {_id: 12, a: NumberDecimal(-15000000000.12345)},
    ]);

    assertDocumentsFromMod(-10000000000, 5000000000, [
        {_id: 2, a: 15000000000},
        {_id: 6, a: 15000000000.12345},
        {_id: 10, a: NumberDecimal(15000000000.12345)},
    ]);

    assertDocumentsFromMod(-10000000000, -5000000000, [
        {_id: 4, a: -15000000000},
        {_id: 8, a: -15000000000.12345},
        {_id: 12, a: NumberDecimal(-15000000000.12345)},
    ]);

    assert(coll.drop());

    // Check truncation of input argument for mod operator.
    assert.writeOK(coll.insert([
        {_id: 1, a: 4.2},
        {_id: 2, a: 4.5},
        {_id: 3, a: 4.7},
        {_id: 4, a: NumberDecimal(4.2)},
        {_id: 5, a: NumberDecimal(4.5)},
        {_id: 6, a: NumberDecimal(4.7)},
    ]));

    assertDocumentsFromMod(4, 0, [
        {_id: 1, a: 4.2},
        {_id: 2, a: 4.5},
        {_id: 3, a: 4.7},
        {_id: 4, a: NumberDecimal(4.2)},
        {_id: 5, a: NumberDecimal(4.5)},
        {_id: 6, a: NumberDecimal(4.7)},
    ]);

    assert(coll.drop());

    // Check more basic mod usage.
    assert.writeOK(coll.insert([
        {_id: 1, str: "abc123", a: 0},
        {_id: 2, str: "xyz123", a: 5},
        {_id: 3, str: "ijk123", a: 12},
        {_id: 4, s: "array", a: [0, 7]},
        {_id: 5, s: "array", a: [1, 7]},
        {_id: 6, s: "array", a: [-5]},
    ]));

    assert.eq(1, coll.find({a: {$mod: [-10, -5]}}).itcount());
    assert.eq(1, coll.find({a: {$mod: [4, -1]}}).itcount());
    assert.eq(4, coll.find({a: {$mod: [5, 0]}}).itcount());
    assert.eq(3, coll.find({a: {$mod: [12, 0]}}).itcount());
    assert.eq(1, coll.find({a: {$mod: [12, 1]}}).itcount());

    assert.writeOK(coll.insert([
        {_id: 7, arr: [1, [2, 3], [[4]]]},
        {_id: 8, arr: [{b: [1, 2, 3]}, {b: [5]}]},
        {_id: 9, arr: [{b: [-1]}, {b: [5]}]},
        {_id: 10, arr: "string"},
    ]));

    // Check nested arrays and dotted paths.
    assert.eq(1, coll.find({arr: {$mod: [1, 0]}}).itcount());
    assert.eq(0, coll.find({arr: {$mod: [4, 0]}}).itcount());
    assert.eq(1, coll.find({"arr.b": {$mod: [3, 0]}}).itcount());
    assert.eq(2, coll.find({"arr.b": {$mod: [5, 0]}}).itcount());
    assert.eq(0, coll.find({"arr.b.c": {$mod: [5, 0]}}).itcount());

    // Check with different data types.
    assert.eq(6, coll.find({a: {$mod: [1, NumberLong(0)]}}).itcount());
    assert.eq(3, coll.find({a: {$mod: [NumberLong(5), NumberDecimal(2.1)]}}).itcount());
    assert.eq(3, coll.find({a: {$mod: [NumberDecimal(5.001), NumberDecimal(2.1)]}}).itcount());
    assert.eq(1, coll.find({a: {$mod: [NumberInt(7), NumberInt(1)]}}).itcount());

    // Check on fields that are not numbers or do not exist.
    assert.eq(0, coll.find({str: {$mod: [10, 1]}}).itcount());
    assert.eq(0, coll.find({s: {$mod: [10, 1]}}).itcount());
    assert.eq(0, coll.find({noField: {$mod: [10, 1]}}).itcount());

    // Check divide by zero.
    assert.commandFailedWithCode(db.runCommand({find: coll.getName(), filter: {a: {$mod: [0, 1]}}}),
                                 ErrorCodes.BadValue);
    assert.commandFailedWithCode(db.runCommand({find: coll.getName(), filter: {a: {$mod: [0, 0]}}}),
                                 ErrorCodes.BadValue);

    // Check failures with different data types.
    assert.commandFailedWithCode(db.runCommand({
        find: coll.getName(),
        filter: {a: {$mod: [NumberDecimal(0.001), NumberDecimal(1.001)]}}
    }),
                                 ErrorCodes.BadValue);
    assert.commandFailedWithCode(db.runCommand({
        find: coll.getName(),
        filter: {a: {$mod: [NumberInt(1), NumberInt(0), NumberInt(0)]}}
    }),
                                 ErrorCodes.BadValue);
    assert.commandFailedWithCode(
        db.runCommand({find: coll.getName(), filter: {a: {$mod: [NumberDecimal(0.1)]}}}),
        ErrorCodes.BadValue);
    assert.commandFailedWithCode(
        db.runCommand(
            {find: coll.getName(), filter: {a: {$mod: [NumberDecimal(0), NumberDecimal(0)]}}}),
        ErrorCodes.BadValue);

    // Check incorrect arity.
    assert.commandFailedWithCode(
        db.runCommand({find: coll.getName(), filter: {a: {$mod: [10, 1, 1]}}}),
        ErrorCodes.BadValue);
    assert.commandFailedWithCode(db.runCommand({find: coll.getName(), filter: {a: {$mod: [10]}}}),
                                 ErrorCodes.BadValue);

    // Check remainder, divisor not a number.
    assert.commandFailedWithCode(
        db.runCommand({find: coll.getName(), filter: {a: {$mod: ["a", 0]}}}), ErrorCodes.BadValue);
    assert.commandFailedWithCode(
        db.runCommand({find: coll.getName(), filter: {a: {$mod: ["a", "b"]}}}),
        ErrorCodes.BadValue);
}());