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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
/**
* Tests some of the find command's semantics with respect to how arrays are handled.
*/
(function() {
"use strict";
load("jstests/aggregation/extras/utils.js"); // arrayEq
const t = db.jstests_arrayfind10;
t.drop();
function runWithAndWithoutIndex(keyPattern, testFunc) {
testFunc();
assert.commandWorked(t.createIndex(keyPattern));
testFunc();
}
assert.commandWorked(t.insert([{a: "foo"}, {a: ["foo"]}, {a: [["foo"]]}, {a: [[["foo"]]]}]));
runWithAndWithoutIndex({a: 1}, () => {
assert(arrayEq(t.find({a: "foo"}, {_id: 0}).toArray(), [{a: "foo"}, {a: ["foo"]}]));
});
assert(t.drop());
assert.commandWorked(t.insert([
{a: [123, "foo"]},
{a: ["foo", 123]},
{a: ["bar", "foo"]},
{a: ["bar", "baz", "foo"]},
{a: ["bar", "baz", 123]}
]));
runWithAndWithoutIndex({a: 1}, () => {
assert(arrayEq(
t.find({a: "foo"}, {_id: 0}).toArray(),
[{a: [123, "foo"]}, {a: ["foo", 123]}, {a: ["bar", "foo"]}, {a: ["bar", "baz", "foo"]}]));
});
assert(t.drop());
assert.commandWorked(t.insert([
{a: [{}, {b: "foo"}]},
{a: [{b: "foo"}, {}]},
{a: [{b: 123}, {b: "foo"}]},
{a: [{b: "foo"}, {b: 123}]},
{a: [{b: "bar"}, {b: "foo"}]},
{a: [{b: "bar"}, {b: "baz"}, {b: "foo"}]},
{a: [{b: "bar"}, {b: "baz"}, {b: 123}]}
]));
runWithAndWithoutIndex({"a.b": 1}, () => {
assert(arrayEq(t.find({"a.b": "foo"}, {_id: 0}).toArray(), [
{a: [{}, {b: "foo"}]},
{a: [{b: "foo"}, {}]},
{a: [{b: 123}, {b: "foo"}]},
{a: [{b: "foo"}, {b: 123}]},
{a: [{b: "bar"}, {b: "foo"}]},
{a: [{b: "bar"}, {b: "baz"}, {b: "foo"}]}
]));
});
assert(t.drop());
assert.commandWorked(
t.insert([{"a": [{"b": [{"c": [5, 7]}]}]}, {"a": [{"b": []}, {"b": [{"c": [5, 7]}]}]}]));
runWithAndWithoutIndex({"a.b": 1}, () => {
assert(arrayEq(t.find({"a.b.c": 5}, {_id: 0}).toArray(),
[{"a": [{"b": [{"c": [5, 7]}]}]}, {"a": [{"b": []}, {"b": [{"c": [5, 7]}]}]}]));
assert(arrayEq(t.find({"a.b.c": {$eq: 5}}, {_id: 0}).toArray(),
[{"a": [{"b": [{"c": [5, 7]}]}]}, {"a": [{"b": []}, {"b": [{"c": [5, 7]}]}]}]));
assert(arrayEq(t.find({"a.b.c": {$in: [5]}}, {_id: 0}).toArray(),
[{"a": [{"b": [{"c": [5, 7]}]}]}, {"a": [{"b": []}, {"b": [{"c": [5, 7]}]}]}]));
assert(arrayEq(t.find({"a.b.c": {$size: 2}}, {_id: 0}).toArray(),
[{"a": [{"b": [{"c": [5, 7]}]}]}, {"a": [{"b": []}, {"b": [{"c": [5, 7]}]}]}]));
assert(arrayEq(t.find({"a.b.c": {$elemMatch: {$eq: 5}}}, {_id: 0}).toArray(),
[{"a": [{"b": [{"c": [5, 7]}]}]}, {"a": [{"b": []}, {"b": [{"c": [5, 7]}]}]}]));
});
assert(t.drop());
assert.commandWorked(t.insert([
{a: 1},
{a: 2},
{a: 3},
{a: [1]},
{a: [1, 2]},
{a: [2]},
{a: [3]},
{a: [3, 1]},
{a: [3, 2]},
{a: [3, 2, 1]},
{a: [[1]]},
{a: [[2]]},
{a: [[3]]},
{a: [[[1]]]},
{a: [[[2]]]},
{a: [[[3]]]},
{a: [1, [[2]]]},
{a: [3, [1]]},
{a: [3, [[2]]]},
{a: [3, [[2]], 1]},
{a: [[1], 2]},
{a: [[1], [2]]},
{a: [[3], 1]},
{a: [[3], 2]},
{a: [[3], 2, 1]},
{a: [[3], [1]]},
{a: [[3], [[2]]]},
{a: [[3], [[2]], 1]}
]));
runWithAndWithoutIndex({a: 1}, () => {
assert(arrayEq(t.find({a: {$eq: [2]}}, {_id: 0}).toArray(),
[{a: [2]}, {a: [[2]]}, {a: [[1], [2]]}]));
assert(arrayEq(t.find({a: {$lt: [2]}}, {_id: 0}).toArray(), [
{a: [1]},
{a: [1, 2]},
{a: [[1]]},
{a: [1, [[2]]]},
{a: [3, [1]]},
{a: [[1], 2]},
{a: [[1], [2]]},
{a: [[3], [1]]}
]));
assert(arrayEq(t.find({a: {$gt: [2]}}, {_id: 0}).toArray(), [
{a: [3]}, {a: [3, 1]}, {a: [3, 2]}, {a: [3, 2, 1]},
{a: [[1]]}, {a: [[2]]}, {a: [[3]]}, {a: [[[1]]]},
{a: [[[2]]]}, {a: [[[3]]]}, {a: [1, [[2]]]}, {a: [3, [1]]},
{a: [3, [[2]]]}, {a: [3, [[2]], 1]}, {a: [[1], 2]}, {a: [[1], [2]]},
{a: [[3], 1]}, {a: [[3], 2]}, {a: [[3], 2, 1]}, {a: [[3], [1]]},
{a: [[3], [[2]]]}, {a: [[3], [[2]], 1]}
]));
assert(arrayEq(t.find({a: {$eq: [1, 2]}}, {_id: 0}).toArray(), [{a: [1, 2]}]));
assert(arrayEq(
t.find({a: {$lt: [1, 2]}}, {_id: 0}).toArray(),
[{a: [1]}, {a: [[1]]}, {a: [3, [1]]}, {a: [[1], 2]}, {a: [[1], [2]]}, {a: [[3], [1]]}]));
assert(arrayEq(t.find({a: {$gt: [1, 2]}}, {_id: 0}).toArray(), [
{a: [2]}, {a: [3]}, {a: [3, 1]}, {a: [3, 2]},
{a: [3, 2, 1]}, {a: [[1]]}, {a: [[2]]}, {a: [[3]]},
{a: [[[1]]]}, {a: [[[2]]]}, {a: [[[3]]]}, {a: [1, [[2]]]},
{a: [3, [1]]}, {a: [3, [[2]]]}, {a: [3, [[2]], 1]}, {a: [[1], 2]},
{a: [[1], [2]]}, {a: [[3], 1]}, {a: [[3], 2]}, {a: [[3], 2, 1]},
{a: [[3], [1]]}, {a: [[3], [[2]]]}, {a: [[3], [[2]], 1]}
]));
});
assert(t.drop());
assert.commandWorked(t.insert([
{a: [1]},
{a: [1, 1]},
{a: [1, 2]},
{a: [1, 2, 3]},
{a: [1, 3]},
{a: [2]},
{a: [2, 1]},
{a: [2, 1, 3]},
{a: [2, 2]},
{a: [2, 3]},
{a: [3, 1]},
{a: [3, 2]},
{a: [3, 3]},
]));
runWithAndWithoutIndex({a: 1}, () => {
assert.eq(1, t.find({a: {$eq: [2, 2]}}).itcount());
assert.eq(8, t.find({a: {$lt: [2, 2]}}).itcount());
assert.eq(9, t.find({a: {$lte: [2, 2]}}).itcount());
assert.eq(4, t.find({a: {$gt: [2, 2]}}).itcount());
assert.eq(5, t.find({a: {$gte: [2, 2]}}).itcount());
assert.eq(1, t.find({a: {$eq: [1]}}).itcount());
assert.eq(0, t.find({a: {$lt: [1]}}).itcount());
assert.eq(1, t.find({a: {$lte: [1]}}).itcount());
assert.eq(12, t.find({a: {$gt: [1]}}).itcount());
assert.eq(13, t.find({a: {$gte: [1]}}).itcount());
assert.eq(0, t.find({a: {$eq: [3]}}).itcount());
assert.eq(10, t.find({a: {$lt: [3]}}).itcount());
assert.eq(10, t.find({a: {$lte: [3]}}).itcount());
assert.eq(3, t.find({a: {$gt: [3]}}).itcount());
assert.eq(3, t.find({a: {$gte: [3]}}).itcount());
assert.eq(1, t.find({a: {$eq: [1, 2, 3]}}).itcount());
assert.eq(3, t.find({a: {$lt: [1, 2, 3]}}).itcount());
assert.eq(4, t.find({a: {$lte: [1, 2, 3]}}).itcount());
assert.eq(9, t.find({a: {$gt: [1, 2, 3]}}).itcount());
assert.eq(10, t.find({a: {$gte: [1, 2, 3]}}).itcount());
});
})();
|