summaryrefslogtreecommitdiff
path: root/jstests/core/orj.js
blob: deee0c4821c47fa85daab8168c53206ec2631a78 (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
// Test nested $or clauses SERVER-2585 SERVER-3192
// @tags: [
//   sbe_incompatible,
// ]

t = db.jstests_orj;
t.drop();

t.save({a: 1, b: 2});

function check() {
    assert.throws(function() {
        t.find({x: 0, $or: "a"}).toArray();
    });
    assert.throws(function() {
        t.find({x: 0, $or: []}).toArray();
    });
    assert.throws(function() {
        t.find({x: 0, $or: ["a"]}).toArray();
    });

    assert.throws(function() {
        t.find({x: 0, $or: [{$or: "a"}]}).toArray();
    });
    assert.throws(function() {
        t.find({x: 0, $or: [{$or: []}]}).toArray();
    });
    assert.throws(function() {
        t.find({x: 0, $or: [{$or: ["a"]}]}).toArray();
    });

    assert.throws(function() {
        t.find({x: 0, $nor: "a"}).toArray();
    });
    assert.throws(function() {
        t.find({x: 0, $nor: []}).toArray();
    });
    assert.throws(function() {
        t.find({x: 0, $nor: ["a"]}).toArray();
    });

    assert.throws(function() {
        t.find({x: 0, $nor: [{$nor: "a"}]}).toArray();
    });
    assert.throws(function() {
        t.find({x: 0, $nor: [{$nor: []}]}).toArray();
    });
    assert.throws(function() {
        t.find({x: 0, $nor: [{$nor: ["a"]}]}).toArray();
    });

    assert.eq(1, t.find({a: 1, b: 2}).itcount());

    assert.eq(1, t.find({a: 1, $or: [{b: 2}]}).itcount());
    assert.eq(0, t.find({a: 1, $or: [{b: 3}]}).itcount());

    assert.eq(1, t.find({a: 1, $or: [{$or: [{b: 2}]}]}).itcount());
    assert.eq(1, t.find({a: 1, $or: [{$or: [{b: 2}]}]}).itcount());
    assert.eq(0, t.find({a: 1, $or: [{$or: [{b: 3}]}]}).itcount());

    assert.eq(1, t.find({$or: [{$or: [{a: 2}, {b: 2}]}]}).itcount());
    assert.eq(1, t.find({$or: [{a: 2}, {$or: [{b: 2}]}]}).itcount());
    assert.eq(1, t.find({$or: [{a: 1}, {$or: [{b: 3}]}]}).itcount());

    assert.eq(1, t.find({$or: [{$or: [{a: 1}, {a: 2}]}, {$or: [{b: 3}, {b: 4}]}]}).itcount());
    assert.eq(1, t.find({$or: [{$or: [{a: 0}, {a: 2}]}, {$or: [{b: 2}, {b: 4}]}]}).itcount());
    assert.eq(0, t.find({$or: [{$or: [{a: 0}, {a: 2}]}, {$or: [{b: 3}, {b: 4}]}]}).itcount());

    assert.eq(1, t.find({a: 1, $and: [{$or: [{$or: [{b: 2}]}]}]}).itcount());
    assert.eq(0, t.find({a: 1, $and: [{$or: [{$or: [{b: 3}]}]}]}).itcount());

    assert.eq(1, t.find({$and: [{$or: [{a: 1}, {a: 2}]}, {$or: [{b: 1}, {b: 2}]}]}).itcount());
    assert.eq(0, t.find({$and: [{$or: [{a: 3}, {a: 2}]}, {$or: [{b: 1}, {b: 2}]}]}).itcount());
    assert.eq(0, t.find({$and: [{$or: [{a: 1}, {a: 2}]}, {$or: [{b: 3}, {b: 1}]}]}).itcount());

    assert.eq(0, t.find({$and: [{$nor: [{a: 1}, {a: 2}]}, {$nor: [{b: 1}, {b: 2}]}]}).itcount());
    assert.eq(0, t.find({$and: [{$nor: [{a: 3}, {a: 2}]}, {$nor: [{b: 1}, {b: 2}]}]}).itcount());
    assert.eq(1, t.find({$and: [{$nor: [{a: 3}, {a: 2}]}, {$nor: [{b: 3}, {b: 1}]}]}).itcount());

    assert.eq(1, t.find({$and: [{$or: [{a: 1}, {a: 2}]}, {$nor: [{b: 1}, {b: 3}]}]}).itcount());
    assert.eq(0, t.find({$and: [{$or: [{a: 3}, {a: 2}]}, {$nor: [{b: 1}, {b: 3}]}]}).itcount());
    assert.eq(0, t.find({$and: [{$or: [{a: 1}, {a: 2}]}, {$nor: [{b: 1}, {b: 2}]}]}).itcount());
}

check();

t.createIndex({a: 1});
check();
t.dropIndexes();

t.createIndex({b: 1});
check();
t.dropIndexes();

t.createIndex({a: 1});
t.createIndex({b: 1});
check();
t.dropIndexes();

t.createIndex({a: 1, b: 1});
check();
t.dropIndexes();

t.createIndex({a: 1});
t.createIndex({b: 1});
t.createIndex({a: 1, b: 1});
check();

function checkHinted(hint) {
    assert.eq(1, t.find({a: 1, b: 2}).hint(hint).itcount());

    assert.eq(1, t.find({a: 1, $or: [{b: 2}]}).hint(hint).itcount());
    assert.eq(0, t.find({a: 1, $or: [{b: 3}]}).hint(hint).itcount());

    assert.eq(1, t.find({a: 1, $or: [{$or: [{b: 2}]}]}).hint(hint).itcount());
    assert.eq(1, t.find({a: 1, $or: [{$or: [{b: 2}]}]}).hint(hint).itcount());
    assert.eq(0, t.find({a: 1, $or: [{$or: [{b: 3}]}]}).hint(hint).itcount());

    assert.eq(1, t.find({$or: [{$or: [{a: 2}, {b: 2}]}]}).hint(hint).itcount());
    assert.eq(1, t.find({$or: [{a: 2}, {$or: [{b: 2}]}]}).hint(hint).itcount());
    assert.eq(1, t.find({$or: [{a: 1}, {$or: [{b: 3}]}]}).hint(hint).itcount());

    assert.eq(
        1, t.find({$or: [{$or: [{a: 1}, {a: 2}]}, {$or: [{b: 3}, {b: 4}]}]}).hint(hint).itcount());
    assert.eq(
        1, t.find({$or: [{$or: [{a: 0}, {a: 2}]}, {$or: [{b: 2}, {b: 4}]}]}).hint(hint).itcount());
    assert.eq(
        0, t.find({$or: [{$or: [{a: 0}, {a: 2}]}, {$or: [{b: 3}, {b: 4}]}]}).hint(hint).itcount());

    assert.eq(1, t.find({a: 1, $and: [{$or: [{$or: [{b: 2}]}]}]}).hint(hint).itcount());
    assert.eq(0, t.find({a: 1, $and: [{$or: [{$or: [{b: 3}]}]}]}).hint(hint).itcount());

    assert.eq(
        1, t.find({$and: [{$or: [{a: 1}, {a: 2}]}, {$or: [{b: 1}, {b: 2}]}]}).hint(hint).itcount());
    assert.eq(
        0, t.find({$and: [{$or: [{a: 3}, {a: 2}]}, {$or: [{b: 1}, {b: 2}]}]}).hint(hint).itcount());
    assert.eq(
        0, t.find({$and: [{$or: [{a: 1}, {a: 2}]}, {$or: [{b: 3}, {b: 1}]}]}).hint(hint).itcount());

    assert.eq(
        0,
        t.find({$and: [{$nor: [{a: 1}, {a: 2}]}, {$nor: [{b: 1}, {b: 2}]}]}).hint(hint).itcount());
    assert.eq(
        0,
        t.find({$and: [{$nor: [{a: 3}, {a: 2}]}, {$nor: [{b: 1}, {b: 2}]}]}).hint(hint).itcount());
    assert.eq(
        1,
        t.find({$and: [{$nor: [{a: 3}, {a: 2}]}, {$nor: [{b: 3}, {b: 1}]}]}).hint(hint).itcount());

    assert.eq(
        1,
        t.find({$and: [{$or: [{a: 1}, {a: 2}]}, {$nor: [{b: 1}, {b: 3}]}]}).hint(hint).itcount());
    assert.eq(
        0,
        t.find({$and: [{$or: [{a: 3}, {a: 2}]}, {$nor: [{b: 1}, {b: 3}]}]}).hint(hint).itcount());
    assert.eq(
        0,
        t.find({$and: [{$or: [{a: 1}, {a: 2}]}, {$nor: [{b: 1}, {b: 2}]}]}).hint(hint).itcount());
}

checkHinted({$natural: 1});
checkHinted({a: 1});
checkHinted({b: 1});
checkHinted({a: 1, b: 1});