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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
// @tags: [
// does_not_support_stepdowns,
// # Explain reports errors from $expr differently in 4.4 and older, so this test assumes that all
// # nodes are at least binary version 4.7.
// requires_fcv_47,
// requires_getmore,
// requires_non_retryable_writes,
// ]
// Tests for $expr in the CRUD commands.
(function() {
"use strict";
load("jstests/libs/sbe_assert_error_override.js"); // For 'assert.errorCodeEq'.
const coll = db.expr;
const hello = db.runCommand("hello");
assert.commandWorked(hello);
const isMongos = (hello.msg === "isdbgrid");
//
// $expr in aggregate.
//
coll.drop();
assert.commandWorked(coll.insert({a: 0}));
assert.eq(1, coll.aggregate([{$match: {$expr: {$eq: ["$a", 0]}}}]).itcount());
assert.throws(function() {
coll.aggregate([{$match: {$expr: {$eq: ["$a", "$$unbound"]}}}]);
});
assert.throws(function() {
coll.aggregate([{$match: {$expr: {$divide: [1, "$a"]}}}]);
});
//
// $expr in count.
//
coll.drop();
assert.commandWorked(coll.insert({a: 0}));
assert.eq(1, coll.find({$expr: {$eq: ["$a", 0]}}).count());
assert.throws(function() {
coll.find({$expr: {$eq: ["$a", "$$unbound"]}}).count();
});
assert.throws(function() {
coll.find({$expr: {$divide: [1, "$a"]}}).count();
});
//
// $expr in distinct.
//
coll.drop();
assert.commandWorked(coll.insert({a: 0}));
assert.eq(1, coll.distinct("a", {$expr: {$eq: ["$a", 0]}}).length);
assert.throws(function() {
coll.distinct("a", {$expr: {$eq: ["$a", "$$unbound"]}});
});
assert.throws(function() {
coll.distinct("a", {$expr: {$divide: [1, "$a"]}});
});
//
// $expr in find.
//
// $expr is allowed in query.
coll.drop();
assert.commandWorked(coll.insert({a: 0}));
assert.eq(1, coll.find({$expr: {$eq: ["$a", 0]}}).itcount());
// $expr with time zone expression across getMore (SERVER-31664).
coll.drop();
assert.commandWorked(coll.insert({a: ISODate("2017-10-01T22:00:00")}));
let res = assert.commandWorked(db.runCommand({
find: coll.getName(),
filter: {$expr: {$eq: [1, {$dayOfMonth: {date: "$a", timezone: "America/New_York"}}]}},
batchSize: 0
}));
assert.eq(0, res.cursor.firstBatch.length);
let cursorId = res.cursor.id;
res = assert.commandWorked(db.runCommand({getMore: cursorId, collection: coll.getName()}));
assert.eq(1, res.cursor.nextBatch.length);
// $expr with unbound variable throws.
assert.throws(function() {
coll.find({$expr: {$eq: ["$a", "$$unbound"]}}).itcount();
});
// $and with $expr child containing an invalid expression throws.
assert.throws(function() {
coll.find({$and: [{a: 0}, {$expr: {$anyElementTrue: undefined}}]}).itcount();
});
// $or with $expr child containing an invalid expression throws.
assert.throws(function() {
coll.find({$or: [{a: 0}, {$expr: {$anyElementTrue: undefined}}]}).itcount();
});
// $nor with $expr child containing an invalid expression throws.
assert.throws(function() {
coll.find({$nor: [{a: 0}, {$expr: {$anyElementTrue: undefined}}]}).itcount();
});
// $expr with division by zero throws.
assert.throws(function() {
coll.find({$expr: {$divide: [1, "$a"]}}).itcount();
});
// $expr is allowed in find with explain.
assert.commandWorked(coll.find({$expr: {$eq: ["$a", 0]}}).explain());
// $expr with unbound variable in find with explain throws.
assert.throws(function() {
coll.find({$expr: {$eq: ["$a", "$$unbound"]}}).explain();
});
// $expr which causes a runtime error should be caught be explain and reported as an error in the
// 'executionSuccess' field.
let explain = coll.find({$expr: {$divide: [1, "$a"]}}).explain("executionStats");
// Accommodate format differences between explain via mongos and explain directly on a mongod.
if (!isMongos) {
assert(explain.hasOwnProperty("executionStats"), explain);
assert.eq(explain.executionStats.executionSuccess, false, explain);
assert.errorCodeEq(explain.executionStats.errorCode, [16609, ErrorCodes.TypeMismatch], explain);
}
// $expr is not allowed in $elemMatch projection.
coll.drop();
assert.commandWorked(coll.insert({a: [{b: 5}]}));
assert.throws(function() {
coll.find({}, {a: {$elemMatch: {$expr: {$eq: ["$b", 5]}}}}).itcount();
});
//
// $expr in findAndModify.
//
// $expr is allowed in the query when upsert=false.
coll.drop();
assert.commandWorked(coll.insert({_id: 0, a: 0}));
assert.eq({_id: 0, a: 0, b: 6},
coll.findAndModify(
{query: {_id: 0, $expr: {$eq: ["$a", 0]}}, update: {$set: {b: 6}}, new: true}));
// $expr with unbound variable throws.
assert.throws(function() {
coll.findAndModify(
{query: {_id: 0, $expr: {$eq: ["$a", "$$unbound"]}}, update: {$set: {b: 6}}});
});
// $expr with division by zero throws.
assert.throws(function() {
coll.findAndModify({query: {_id: 0, $expr: {$divide: [1, "$a"]}}, update: {$set: {b: 6}}});
});
// $expr is not allowed in the query when upsert=true.
coll.drop();
assert.commandWorked(coll.insert({_id: 0, a: 0}));
assert.throws(function() {
coll.findAndModify(
{query: {_id: 0, $expr: {$eq: ["$a", 0]}}, update: {$set: {b: 6}}, upsert: true});
});
// $expr is not allowed in $pull filter.
coll.drop();
assert.commandWorked(coll.insert({_id: 0, a: [{b: 5}]}));
assert.throws(function() {
coll.findAndModify({query: {_id: 0}, update: {$pull: {a: {$expr: {$eq: ["$b", 5]}}}}});
});
// $expr is not allowed in arrayFilters.
if (db.getMongo().writeMode() === "commands") {
coll.drop();
assert.commandWorked(coll.insert({_id: 0, a: [{b: 5}]}));
assert.throws(function() {
coll.findAndModify({
query: {_id: 0},
update: {$set: {"a.$[i].b": 6}},
arrayFilters: [{"i.b": 5, $expr: {$eq: ["$i.b", 5]}}]
});
});
}
//
// $expr in the $geoNear stage.
//
coll.drop();
assert.commandWorked(coll.insert({geo: {type: "Point", coordinates: [0, 0]}, a: 0}));
assert.commandWorked(coll.createIndex({geo: "2dsphere"}));
assert.eq(1,
coll.aggregate({
$geoNear: {
near: {type: "Point", coordinates: [0, 0]},
distanceField: "dist",
spherical: true,
query: {$expr: {$eq: ["$a", 0]}}
}
})
.toArray()
.length);
assert.throws(() => coll.aggregate({
$geoNear: {
near: {type: "Point", coordinates: [0, 0]},
distanceField: "dist",
spherical: true,
query: {$expr: {$eq: ["$a", "$$unbound"]}}
}
}));
assert.throws(() => coll.aggregate({
$geoNear: {
near: {type: "Point", coordinates: [0, 0]},
distanceField: "dist",
spherical: true,
query: {$expr: {$divide: [1, "$a"]}}
}
}));
//
// $expr in mapReduce.
//
coll.drop();
assert.commandWorked(coll.insert({a: 0}));
let mapReduceOut = coll.mapReduce(
function() {
emit(this.a, 1);
},
function(key, values) {
return Array.sum(values);
},
{out: {inline: 1}, query: {$expr: {$eq: ["$a", 0]}}});
assert.commandWorked(mapReduceOut);
assert.eq(mapReduceOut.results.length, 1, tojson(mapReduceOut));
assert.throws(function() {
coll.mapReduce(
function() {
emit(this.a, 1);
},
function(key, values) {
return Array.sum(values);
},
{out: {inline: 1}, query: {$expr: {$eq: ["$a", "$$unbound"]}}});
});
assert.throws(function() {
coll.mapReduce(
function() {
emit(this.a, 1);
},
function(key, values) {
return Array.sum(values);
},
{out: {inline: 1}, query: {$expr: {$divide: [1, "$a"]}}});
});
//
// $expr in remove.
//
coll.drop();
assert.commandWorked(coll.insert({_id: 0, a: 0}));
let writeRes = coll.remove({_id: 0, $expr: {$eq: ["$a", 0]}});
assert.commandWorked(writeRes);
assert.eq(1, writeRes.nRemoved);
assert.writeError(coll.remove({_id: 0, $expr: {$eq: ["$a", "$$unbound"]}}));
assert.commandWorked(coll.insert({_id: 0, a: 0}));
assert.writeError(coll.remove({_id: 0, $expr: {$divide: [1, "$a"]}}));
// Any writes preceding the write that fails to parse are executed.
coll.drop();
assert.commandWorked(coll.insert({_id: 0}));
assert.commandWorked(coll.insert({_id: 1}));
writeRes = db.runCommand({
delete: coll.getName(),
deletes: [{q: {_id: 0}, limit: 1}, {q: {$expr: "$$unbound"}, limit: 1}]
});
assert.commandWorkedIgnoringWriteErrors(writeRes);
assert.eq(writeRes.writeErrors[0].code, 17276, tojson(writeRes));
assert.eq(writeRes.n, 1, tojson(writeRes));
//
// $expr in update.
//
// $expr is allowed in the query when upsert=false.
coll.drop();
assert.commandWorked(coll.insert({_id: 0, a: 0}));
assert.commandWorked(coll.update({_id: 0, $expr: {$eq: ["$a", 0]}}, {$set: {b: 6}}));
assert.eq({_id: 0, a: 0, b: 6}, coll.findOne({_id: 0}));
// $expr with unbound variable fails.
assert.writeError(coll.update({_id: 0, $expr: {$eq: ["$a", "$$unbound"]}}, {$set: {b: 6}}));
// $expr with division by zero fails.
assert.writeError(coll.update({_id: 0, $expr: {$divide: [1, "$a"]}}, {$set: {b: 6}}));
// $expr is not allowed in the query when upsert=true.
coll.drop();
assert.commandWorked(coll.insert({_id: 0, a: 5}));
assert.writeError(coll.update({_id: 0, $expr: {$eq: ["$a", 5]}}, {$set: {b: 6}}, {upsert: true}));
// $expr is not allowed in $pull filter.
coll.drop();
assert.commandWorked(coll.insert({_id: 0, a: [{b: 5}]}));
assert.writeError(coll.update({_id: 0}, {$pull: {a: {$expr: {$eq: ["$b", 5]}}}}));
// $expr is not allowed in arrayFilters.
if (db.getMongo().writeMode() === "commands") {
coll.drop();
assert.commandWorked(coll.insert({_id: 0, a: [{b: 5}]}));
assert.writeError(coll.update({_id: 0},
{$set: {"a.$[i].b": 6}},
{arrayFilters: [{"i.b": 5, $expr: {$eq: ["$i.b", 5]}}]}));
}
// Any writes preceding the write that fails to parse are executed.
coll.drop();
assert.commandWorked(coll.insert({_id: 0}));
assert.commandWorked(coll.insert({_id: 1}));
writeRes = db.runCommand({
update: coll.getName(),
updates: [{q: {_id: 0}, u: {$set: {b: 6}}}, {q: {$expr: "$$unbound"}, u: {$set: {b: 6}}}]
});
assert.commandWorkedIgnoringWriteErrors(writeRes);
assert.eq(writeRes.writeErrors[0].code, 17276, tojson(writeRes));
assert.eq(writeRes.n, 1, tojson(writeRes));
})();
|