summaryrefslogtreecommitdiff
path: root/jstests/core/null_query_semantics.js
blob: ea03ba082caccbaa840d8cc713dbcf336779201c (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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
// Tests the behavior of queries with a {$eq: null} or {$ne: null} predicate.
//
// @tags: [
//   # This file includes tests for SERVER-21929, which is fixed in 5.0 but not earlier versions.
//   # Therefore, for this test to run all nodes must be at least 5.0.
//   requires_fcv_50,
// ]
(function() {
"use strict";

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

function extractAValues(results) {
    return results.map(function(res) {
        if (!res.hasOwnProperty("a")) {
            return {};
        }
        return {a: res.a};
    });
}

function testNullSemantics(coll) {
    // For the first portion of the test, only insert documents without arrays. This will avoid
    // making the indexes multi-key, which may allow an index to be used to answer the queries.
    assert.commandWorked(coll.insert([
        {_id: "a_empty_subobject", a: {}},
        {_id: "a_null", a: null},
        {_id: "a_number", a: 4},
        {_id: "a_subobject_b_not_null", a: {b: "hi"}},
        {_id: "a_subobject_b_null", a: {b: null}},
        {_id: "a_subobject_b_undefined", a: {b: undefined}},
        {_id: "a_undefined", a: undefined},
        {_id: "no_a"},
    ]));

    // Throughout this test we will run queries with a projection which may allow the planner to
    // consider an index-only plan. Checking the results of those queries will test that the
    // query system will never choose such an optimization if it is incorrect.
    const projectToOnlyA = {_id: 0, a: 1};
    const projectToOnlyADotB = {_id: 0, "a.b": 1};

    // Test the semantics of the query {a: {$eq: null}}.
    (function testBasicNullQuery() {
        const noProjectResults = coll.find({a: {$eq: null}}).toArray();
        const expected =
            [{_id: "a_null", a: null}, {_id: "a_undefined", a: undefined}, {_id: "no_a"}];
        assert(resultsEq(expected, noProjectResults), tojson(noProjectResults));

        const projectResults = coll.find({a: {$eq: null}}, projectToOnlyA).toArray();
        assert(resultsEq(projectResults, extractAValues(expected)), tojson(projectResults));
    }());

    // Test the semantics of the query {a: {$ne: null}}.
    (function testBasicNotEqualsNullQuery() {
        const noProjectResults = coll.find({a: {$ne: null}}).toArray();
        const expected = [
            {_id: "a_empty_subobject", a: {}},
            {_id: "a_number", a: 4},
            {_id: "a_subobject_b_not_null", a: {b: "hi"}},
            {_id: "a_subobject_b_null", a: {b: null}},
            {_id: "a_subobject_b_undefined", a: {b: undefined}},
        ];
        assert(resultsEq(noProjectResults, expected), tojson(noProjectResults));

        const projectResults = coll.find({a: {$ne: null}}, projectToOnlyA).toArray();
        assert(resultsEq(projectResults, extractAValues(expected)), tojson(projectResults));
    }());

    // Test the semantics of the query {a: {$in: [null, <number>]}}.
    (function testInNullQuery() {
        const query = {a: {$in: [null, 4]}};
        const noProjectResults = coll.find(query).toArray();
        const expected = [
            {_id: "a_null", a: null},
            {_id: "a_number", a: 4},
            {_id: "a_undefined", a: undefined},
            {_id: "no_a"},
        ];

        assert(resultsEq(noProjectResults, expected), noProjectResults);

        const projectResults = coll.find(query, projectToOnlyA).toArray();
        assert(resultsEq(projectResults, extractAValues(expected)), projectResults);
    }());

    // Test the semantics of the query {$or: [{a: null}, {a: <number>}]}.
    (function testOrNullQuery() {
        const query = {$or: [{a: null}, {a: 4}]};
        const noProjectResults = coll.find(query).toArray();
        const expected = [
            {_id: "a_null", a: null},
            {_id: "a_number", a: 4},
            {_id: "a_undefined", a: undefined},
            {_id: "no_a"},
        ];

        assert(resultsEq(noProjectResults, expected), noProjectResults);

        const projectResults = coll.find(query, projectToOnlyA).toArray();
        assert(resultsEq(projectResults, extractAValues(expected)), projectResults);
    }());

    // Test the semantics of the query {a: {$nin: [null, <number>]}}.
    (function testNotInNullQuery() {
        const query = {a: {$nin: [null, 4]}};
        const noProjectResults = coll.find(query).toArray();
        const expected = [
            {_id: "a_empty_subobject", a: {}},
            {_id: "a_subobject_b_not_null", a: {b: "hi"}},
            {_id: "a_subobject_b_null", a: {b: null}},
            {_id: "a_subobject_b_undefined", a: {b: undefined}},
        ];

        assert(resultsEq(noProjectResults, expected), noProjectResults);

        const projectResults = coll.find(query, projectToOnlyA).toArray();
        assert(resultsEq(projectResults, extractAValues(expected)), projectResults);
    }());

    (function testNotInNullAndRegexQuery() {
        // While $nin: [null, ...] can be indexed, $nin: [<regex>] cannot. Ensure that we get
        // the correct results in this case.
        const query = {a: {$nin: [null, /^hi.*/]}};
        const noProjectResults = coll.find(query).toArray();
        const expected = [
            {_id: "a_empty_subobject", a: {}},
            {_id: "a_number", a: 4},
            {_id: "a_subobject_b_not_null", a: {b: "hi"}},
            {_id: "a_subobject_b_null", a: {b: null}},
            {_id: "a_subobject_b_undefined", a: {b: undefined}},
        ];
        assert(resultsEq(noProjectResults, expected), tojson(noProjectResults));

        const projectResults = coll.find(query, projectToOnlyA).toArray();
        assert(resultsEq(projectResults, extractAValues(expected)), projectResults);
    }());

    (function testExistsFalse() {
        const noProjectResults = coll.find({a: {$exists: false}}).toArray();
        const expected = [
            {_id: "no_a"},
        ];
        assert(resultsEq(noProjectResults, expected), tojson(noProjectResults));

        const projectResults = coll.find({a: {$exists: false}}, projectToOnlyA).toArray();
        assert(resultsEq(projectResults, extractAValues(expected)), tojson(projectResults));
    }());

    // Test the semantics of the query {"a.b": {$eq: null}}.
    (function testDottedEqualsNull() {
        const noProjectResults = coll.find({"a.b": {$eq: null}}).toArray();
        assert(resultsEq(noProjectResults,
                         [
                             {_id: "a_empty_subobject", a: {}},
                             {_id: "a_null", a: null},
                             {_id: "a_number", a: 4},
                             {_id: "a_subobject_b_null", a: {b: null}},
                             {_id: "a_subobject_b_undefined", a: {b: undefined}},
                             {_id: "a_undefined", a: undefined},
                             {_id: "no_a"}
                         ]),
               tojson(noProjectResults));

        const projectResults = coll.find({"a.b": {$eq: null}}, projectToOnlyADotB).toArray();
        assert(resultsEq(projectResults,
                         [{a: {}}, {}, {}, {a: {b: null}}, {a: {b: undefined}}, {}, {}]),
               tojson(projectResults));
    }());

    // Test the semantics of the query {"a.b": {$ne: null}}.
    (function testDottedNotEqualsNull() {
        const noProjectResults = coll.find({"a.b": {$ne: null}}).toArray();
        assert(resultsEq(noProjectResults, [{_id: "a_subobject_b_not_null", a: {b: "hi"}}]),
               tojson(noProjectResults));

        const projectResults = coll.find({"a.b": {$ne: null}}, projectToOnlyADotB).toArray();
        assert(resultsEq(projectResults, [{a: {b: "hi"}}]), tojson(projectResults));
    }());

    (function testDottedExistsFalse() {
        const noProjectResults = coll.find({"a.b": {$exists: false}}).toArray();
        const expected = [
            {_id: "no_a"},
            {_id: "a_empty_subobject", a: {}},
            {_id: "a_null", a: null},
            {_id: "a_number", a: 4},
            {_id: "a_undefined", a: undefined},
        ];
        assert(resultsEq(noProjectResults, expected), tojson(noProjectResults));

        const projectResults = coll.find({"a.b": {$exists: false}}, projectToOnlyADotB).toArray();
        assert(resultsEq(projectResults, [{}, {a: {}}, {}, {}, {}]), tojson(projectResults));
    }());

    // Test similar queries, but with an $elemMatch. These queries should have no results since
    // an $elemMatch requires an array.
    (function testElemMatchQueriesWithNoArrays() {
        for (let elemMatchQuery of [{a: {$elemMatch: {$eq: null}}},
                                    {a: {$elemMatch: {$ne: null}}},
                                    {"a.b": {$elemMatch: {$eq: null}}},
                                    {"a.b": {$elemMatch: {$ne: null}}},
                                    {a: {$elemMatch: {b: {$eq: null}}}},
                                    {a: {$elemMatch: {b: {$ne: null}}}},
        ]) {
            const noProjectResults = coll.find(elemMatchQuery).toArray();
            assert(resultsEq(noProjectResults, []),
                   `Expected no results for query ${tojson(elemMatchQuery)}, got ` +
                       tojson(noProjectResults));

            let projectResults = coll.find(elemMatchQuery, projectToOnlyA).toArray();
            assert(resultsEq(projectResults, []),
                   `Expected no results for query ${tojson(elemMatchQuery)}, got ` +
                       tojson(projectResults));

            projectResults = coll.find(elemMatchQuery, projectToOnlyADotB).toArray();
            assert(resultsEq(projectResults, []),
                   `Expected no results for query ${tojson(elemMatchQuery)}, got ` +
                       tojson(projectResults));
        }
    }());

    // An index which includes "a" or a sub-path of "a" will become multi-key after this insert.
    const writeResult = coll.insert([
        {_id: "a_double_array", a: [[]]},
        {_id: "a_empty_array", a: []},
        {_id: "a_object_array_all_b_nulls", a: [{b: null}, {b: undefined}, {b: null}, {}]},
        {_id: "a_object_array_no_b_nulls", a: [{b: 1}, {b: 3}, {b: "string"}]},
        {_id: "a_object_array_some_b_nulls", a: [{b: null}, {b: 3}, {b: null}]},
        {_id: "a_object_array_some_b_undefined", a: [{b: undefined}, {b: 3}]},
        {_id: "a_object_array_some_b_missing", a: [{b: 3}, {}]},
        {_id: "a_value_array_all_nulls", a: [null, null]},
        {_id: "a_value_array_no_nulls", a: [1, "string", 4]},
        {_id: "a_value_array_with_null", a: [1, "string", null, 4]},
        {_id: "a_value_array_with_undefined", a: [1, "string", undefined, 4]},
    ]);
    if (writeResult.hasWriteErrors()) {
        // We're testing a hashed index which is incompatible with arrays. Skip the multi-key
        // portion of this test for this index.
        assert.eq(writeResult.getWriteErrors().length, 1, tojson(writeResult));
        assert.eq(writeResult.getWriteErrors()[0].code, 16766, tojson(writeResult));
        return;
    }
    assert.commandWorked(writeResult);

    // Test the semantics of the query {a: {$eq: null}}.
    (function testBasicNullQuery() {
        const noProjectResults = coll.find({a: {$eq: null}}).toArray();
        const expected = [
            {_id: "a_null", a: null},
            {_id: "a_undefined", a: undefined},
            {_id: "a_value_array_all_nulls", a: [null, null]},
            {_id: "a_value_array_with_null", a: [1, "string", null, 4]},
            {_id: "a_value_array_with_undefined", a: [1, "string", undefined, 4]},
            {_id: "no_a"},
        ];
        assert(resultsEq(noProjectResults, expected), tojson(noProjectResults));

        const projectResults = coll.find({a: {$eq: null}}, projectToOnlyA).toArray();
        assert(resultsEq(projectResults, extractAValues(expected)), tojson(projectResults));
    }());

    // Test the semantics of the query {a: {$ne: null}}.
    (function testBasicNotEqualsNullQuery() {
        const noProjectResults = coll.find({a: {$ne: null}}).toArray();
        const expected = [
            {_id: "a_double_array", a: [[]]},
            {_id: "a_empty_array", a: []},
            {_id: "a_empty_subobject", a: {}},
            {_id: "a_number", a: 4},
            {_id: "a_object_array_all_b_nulls", a: [{b: null}, {b: undefined}, {b: null}, {}]},
            {_id: "a_object_array_no_b_nulls", a: [{b: 1}, {b: 3}, {b: "string"}]},
            {_id: "a_object_array_some_b_nulls", a: [{b: null}, {b: 3}, {b: null}]},
            {_id: "a_object_array_some_b_undefined", a: [{b: undefined}, {b: 3}]},
            {_id: "a_object_array_some_b_missing", a: [{b: 3}, {}]},
            {_id: "a_subobject_b_not_null", a: {b: "hi"}},
            {_id: "a_subobject_b_null", a: {b: null}},
            {_id: "a_subobject_b_undefined", a: {b: undefined}},
            {_id: "a_value_array_no_nulls", a: [1, "string", 4]},
        ];
        assert(resultsEq(noProjectResults, expected), tojson(noProjectResults));

        const projectResults = coll.find({a: {$ne: null}}, projectToOnlyA).toArray();
        assert(resultsEq(projectResults, extractAValues(expected)), tojson(projectResults));
    }());

    // Test the semantics of the query {a: {$not: {$gte: null}}}.
    (function testBasicNotEqualsGTENullQuery() {
        const noProjectResults = coll.find({a: {$not: {$gte: null}}}).toArray();
        const expected = [
            {_id: "a_double_array", a: [[]]},
            {_id: "a_empty_array", a: []},
            {_id: "a_empty_subobject", a: {}},
            {_id: "a_number", a: 4},
            {_id: "a_object_array_all_b_nulls", a: [{b: null}, {b: undefined}, {b: null}, {}]},
            {_id: "a_object_array_no_b_nulls", a: [{b: 1}, {b: 3}, {b: "string"}]},
            {_id: "a_object_array_some_b_nulls", a: [{b: null}, {b: 3}, {b: null}]},
            {_id: "a_object_array_some_b_undefined", a: [{b: undefined}, {b: 3}]},
            {_id: "a_object_array_some_b_missing", a: [{b: 3}, {}]},
            {_id: "a_subobject_b_not_null", a: {b: "hi"}},
            {_id: "a_subobject_b_null", a: {b: null}},
            {_id: "a_subobject_b_undefined", a: {b: undefined}},
            {_id: "a_value_array_no_nulls", a: [1, "string", 4]},
        ];
        assert(resultsEq(noProjectResults, expected), tojson(noProjectResults));
    }());

    // Test the semantics of the query {a: {$in: [null, <number>]}}.
    (function testInNullQuery() {
        const query = {a: {$in: [null, 75]}};
        const noProjectResults = coll.find(query).toArray();
        const expected = [
            {_id: "a_null", a: null},
            {_id: "a_undefined", a: undefined},
            {_id: "no_a"},
            {_id: "a_value_array_all_nulls", a: [null, null]},
            {_id: "a_value_array_with_null", a: [1, "string", null, 4]},
            {_id: "a_value_array_with_undefined", a: [1, "string", undefined, 4]},
        ];

        assert(resultsEq(noProjectResults, expected), noProjectResults);

        const projectResults = coll.find(query, projectToOnlyA).toArray();
        assert(resultsEq(projectResults, extractAValues(expected)), projectResults);
    }());

    // Test the semantics of the query {$or: [{a: null}, {a: <number>}]}.
    (function testOrNullQuery() {
        const query = {$or: [{a: null}, {a: 75}]};
        const noProjectResults = coll.find(query).toArray();
        const expected = [
            {_id: "a_null", a: null},
            {_id: "a_undefined", a: undefined},
            {_id: "no_a"},
            {_id: "a_value_array_all_nulls", a: [null, null]},
            {_id: "a_value_array_with_null", a: [1, "string", null, 4]},
            {_id: "a_value_array_with_undefined", a: [1, "string", undefined, 4]},
        ];

        assert(resultsEq(noProjectResults, expected), noProjectResults);

        const projectResults = coll.find(query, projectToOnlyA).toArray();
        assert(resultsEq(projectResults, extractAValues(expected)), projectResults);
    }());

    // Test the semantics of the query {a: {$nin: [null, <number>]}}.
    (function testNotInNullQuery() {
        const query = {a: {$nin: [null, 75]}};
        const noProjectResults = coll.find(query).toArray();
        const expected = [
            {_id: "a_empty_subobject", a: {}},
            {_id: "a_number", a: 4},
            {_id: "a_subobject_b_not_null", a: {b: "hi"}},
            {_id: "a_subobject_b_null", a: {b: null}},
            {_id: "a_subobject_b_undefined", a: {b: undefined}},

            {_id: "a_double_array", a: [[]]},
            {_id: "a_empty_array", a: []},
            {_id: "a_object_array_all_b_nulls", a: [{b: null}, {b: undefined}, {b: null}, {}]},
            {_id: "a_object_array_no_b_nulls", a: [{b: 1}, {b: 3}, {b: "string"}]},
            {_id: "a_object_array_some_b_nulls", a: [{b: null}, {b: 3}, {b: null}]},
            {_id: "a_object_array_some_b_undefined", a: [{b: undefined}, {b: 3}]},
            {_id: "a_object_array_some_b_missing", a: [{b: 3}, {}]},
            {_id: "a_value_array_no_nulls", a: [1, "string", 4]},
        ];

        assert(resultsEq(noProjectResults, expected), noProjectResults);

        const projectResults = coll.find(query, projectToOnlyA).toArray();
        assert(resultsEq(projectResults, extractAValues(expected)), projectResults);
    }());

    // Test the semantics of the query {a: {$nin: [null, []]}}.
    (function testNotInNullAndEmptyArrayQuery() {
        const query = {a: {$nin: [null, []]}};
        const noProjectResults = coll.find(query).toArray();
        const expected = [
            // Documents excluded by the query predicate are commented
            // out below.
            {_id: "a_empty_subobject", a: {}},
            //{_id: "a_null", a: null},
            {_id: "a_number", a: 4},
            {_id: "a_subobject_b_not_null", a: {b: "hi"}},
            {_id: "a_subobject_b_null", a: {b: null}},
            {_id: "a_subobject_b_undefined", a: {b: undefined}},
            //{_id: "a_undefined", a: undefined},
            //{_id: "no_a"},

            //{_id: "a_double_array", a: [[]]},
            //{_id: "a_empty_array", a: []},
            {_id: "a_object_array_all_b_nulls", a: [{b: null}, {b: undefined}, {b: null}, {}]},
            {_id: "a_object_array_no_b_nulls", a: [{b: 1}, {b: 3}, {b: "string"}]},
            {_id: "a_object_array_some_b_nulls", a: [{b: null}, {b: 3}, {b: null}]},
            {_id: "a_object_array_some_b_undefined", a: [{b: undefined}, {b: 3}]},
            {_id: "a_object_array_some_b_missing", a: [{b: 3}, {}]},
            //{_id: "a_value_array_all_nulls", a: [null, null]},
            {_id: "a_value_array_no_nulls", a: [1, "string", 4]},
            //{_id: "a_value_array_with_null", a: [1, "string", null, 4]},
            //{_id: "a_value_array_with_undefined", a: [1, "string", undefined, 4]},
        ];

        assert(resultsEq(noProjectResults, expected), noProjectResults);

        const projectResults = coll.find(query, projectToOnlyA).toArray();
        assert(resultsEq(projectResults, extractAValues(expected)), projectResults);
    }());

    (function testNotInNullAndRegexQuery() {
        const query = {a: {$nin: [null, /^str.*/]}};
        const noProjectResults = coll.find(query).toArray();
        const expected = [
            {_id: "a_empty_subobject", a: {}},
            {_id: "a_number", a: 4},
            {_id: "a_subobject_b_not_null", a: {b: "hi"}},
            {_id: "a_subobject_b_null", a: {b: null}},
            {_id: "a_subobject_b_undefined", a: {b: undefined}},

            {_id: "a_double_array", a: [[]]},
            {_id: "a_empty_array", a: []},
            {_id: "a_object_array_all_b_nulls", a: [{b: null}, {b: undefined}, {b: null}, {}]},
            {_id: "a_object_array_no_b_nulls", a: [{b: 1}, {b: 3}, {b: "string"}]},
            {_id: "a_object_array_some_b_nulls", a: [{b: null}, {b: 3}, {b: null}]},
            {_id: "a_object_array_some_b_undefined", a: [{b: undefined}, {b: 3}]},
            {_id: "a_object_array_some_b_missing", a: [{b: 3}, {}]},
        ];

        assert(resultsEq(noProjectResults, expected), noProjectResults);

        const projectResults = coll.find(query, projectToOnlyA).toArray();
        assert(resultsEq(projectResults, extractAValues(expected)), projectResults);
    }());

    // Test the results of similar queries with an $elemMatch.
    (function testElemMatchValue() {
        // Test $elemMatch with equality to null.
        let noProjectResults = coll.find({a: {$elemMatch: {$eq: null}}}).toArray();
        const expectedEqualToNull = [
            {_id: "a_value_array_all_nulls", a: [null, null]},
            {_id: "a_value_array_with_null", a: [1, "string", null, 4]},
            {_id: "a_value_array_with_undefined", a: [1, "string", undefined, 4]},
        ];
        assert(resultsEq(noProjectResults, expectedEqualToNull), tojson(noProjectResults));

        let projectResults = coll.find({a: {$elemMatch: {$eq: null}}}, projectToOnlyA).toArray();
        assert(resultsEq(projectResults, extractAValues(expectedEqualToNull)),
               tojson(projectResults));

        // Test $elemMatch with not equal to null.
        noProjectResults = coll.find({a: {$elemMatch: {$ne: null}}}).toArray();
        const expectedNotEqualToNull = [
            {_id: "a_double_array", a: [[]]},
            {_id: "a_object_array_all_b_nulls", a: [{b: null}, {b: undefined}, {b: null}, {}]},
            {_id: "a_object_array_no_b_nulls", a: [{b: 1}, {b: 3}, {b: "string"}]},
            {_id: "a_object_array_some_b_nulls", a: [{b: null}, {b: 3}, {b: null}]},
            {_id: "a_object_array_some_b_undefined", a: [{b: undefined}, {b: 3}]},
            {_id: "a_object_array_some_b_missing", a: [{b: 3}, {}]},
            {_id: "a_value_array_no_nulls", a: [1, "string", 4]},
            {_id: "a_value_array_with_undefined", a: [1, "string", undefined, 4]},
            {_id: "a_value_array_with_null", a: [1, "string", null, 4]},
        ];
        assert(resultsEq(noProjectResults, expectedNotEqualToNull), tojson(noProjectResults));

        projectResults = coll.find({a: {$elemMatch: {$ne: null}}}, projectToOnlyA).toArray();
        assert(resultsEq(projectResults, extractAValues(expectedNotEqualToNull)),
               tojson(projectResults));
    }());

    // Test the semantics of the query {"a.b": {$eq: null}}. The semantics here are to return
    // those documents which have one of the following properties:
    //  - A non-object, non-array value for "a"
    //  - A subobject "a" with a missing, null, or undefined value for "b"
    //  - An array which has at least one object in it which has a missing, null, or undefined
    //    value for "b".
    (function testDottedEqualsNull() {
        const noProjectResults = coll.find({"a.b": {$eq: null}}).toArray();
        assert(
            resultsEq(noProjectResults,
                      [
                          {_id: "a_empty_subobject", a: {}},
                          {_id: "a_null", a: null},
                          {_id: "a_number", a: 4},
                          {_id: "a_subobject_b_null", a: {b: null}},
                          {_id: "a_subobject_b_undefined", a: {b: undefined}},
                          {_id: "a_undefined", a: undefined},
                          {_id: "no_a"},
                          {
                              _id: "a_object_array_all_b_nulls",
                              a: [{b: null}, {b: undefined}, {b: null}, {}]
                          },
                          {_id: "a_object_array_some_b_nulls", a: [{b: null}, {b: 3}, {b: null}]},
                          {_id: "a_object_array_some_b_undefined", a: [{b: undefined}, {b: 3}]},
                          {_id: "a_object_array_some_b_missing", a: [{b: 3}, {}]},
                      ]),
            tojson(noProjectResults));

        const projectResults = coll.find({"a.b": {$eq: null}}, projectToOnlyADotB).toArray();
        assert(resultsEq(projectResults,
                         [
                             {a: {}},
                             {},
                             {},
                             {a: {b: null}},
                             {a: {b: undefined}},
                             {},
                             {},
                             {a: [{b: null}, {b: undefined}, {b: null}, {}]},
                             {a: [{b: null}, {b: 3}, {b: null}]},
                             {a: [{b: undefined}, {b: 3}]},
                             {a: [{b: 3}, {}]},
                         ]),
               tojson(projectResults));
    }());

    // Test the semantics of the query {"a.b": {$ne: null}}.
    (function testDottedNotEqualsNull() {
        const noProjectResults = coll.find({"a.b": {$ne: null}}).toArray();
        assert(resultsEq(noProjectResults,
                         [
                             {_id: "a_subobject_b_not_null", a: {b: "hi"}},
                             {_id: "a_double_array", a: [[]]},
                             {_id: "a_empty_array", a: []},
                             {_id: "a_object_array_no_b_nulls", a: [{b: 1}, {b: 3}, {b: "string"}]},
                             {_id: "a_value_array_all_nulls", a: [null, null]},
                             {_id: "a_value_array_no_nulls", a: [1, "string", 4]},
                             {_id: "a_value_array_with_null", a: [1, "string", null, 4]},
                             {_id: "a_value_array_with_undefined", a: [1, "string", undefined, 4]}
                         ]),
               tojson(noProjectResults));

        const projectResults = coll.find({"a.b": {$ne: null}}, projectToOnlyADotB).toArray();
        assert(resultsEq(projectResults,
                         [
                             {a: {b: "hi"}},
                             {a: [[]]},
                             {a: []},
                             {a: [{b: 1}, {b: 3}, {b: "string"}]},
                             {a: []},
                             {a: []},
                             {a: []},
                             {a: []}
                         ]),
               tojson(projectResults));
    }());

    // Test the semantics of the query {a.b: {$in: [null, <number>]}}.
    (function testDottedInNullQuery() {
        const query = {"a.b": {$in: [null, 75]}};
        const noProjectResults = coll.find(query).toArray();
        const expected = [
            {_id: "a_empty_subobject", a: {}},
            {_id: "a_null", a: null},
            {_id: "a_number", a: 4},
            {_id: "a_subobject_b_null", a: {b: null}},
            {_id: "a_subobject_b_undefined", a: {b: undefined}},
            {_id: "a_undefined", a: undefined},
            {_id: "no_a"},
            {_id: "a_object_array_all_b_nulls", a: [{b: null}, {b: undefined}, {b: null}, {}]},
            {_id: "a_object_array_some_b_nulls", a: [{b: null}, {b: 3}, {b: null}]},
            {_id: "a_object_array_some_b_undefined", a: [{b: undefined}, {b: 3}]},
            {_id: "a_object_array_some_b_missing", a: [{b: 3}, {}]},
        ];

        assert(resultsEq(noProjectResults, expected), noProjectResults);
    }());

    // Test the semantics of the query {$or: [{a.b: null}, {a.b: <number>}]}.
    (function testDottedOrNullQuery() {
        const query = {$or: [{"a.b": null}, {"a.b": 75}]};
        const noProjectResults = coll.find(query).toArray();
        const expected = [
            {_id: "a_empty_subobject", a: {}},
            {_id: "a_null", a: null},
            {_id: "a_number", a: 4},
            {_id: "a_subobject_b_null", a: {b: null}},
            {_id: "a_subobject_b_undefined", a: {b: undefined}},
            {_id: "a_undefined", a: undefined},
            {_id: "no_a"},
            {_id: "a_object_array_all_b_nulls", a: [{b: null}, {b: undefined}, {b: null}, {}]},
            {_id: "a_object_array_some_b_nulls", a: [{b: null}, {b: 3}, {b: null}]},
            {_id: "a_object_array_some_b_undefined", a: [{b: undefined}, {b: 3}]},
            {_id: "a_object_array_some_b_missing", a: [{b: 3}, {}]},
        ];

        assert(resultsEq(noProjectResults, expected), noProjectResults);
    }());

    // Test the semantics of the query {a.b: {$nin: [null, <number>]}}.
    (function testDottedNotInNullQuery() {
        const query = {"a.b": {$nin: [null, 75]}};
        const noProjectResults = coll.find(query).toArray();
        const expected = [
            {_id: "a_subobject_b_not_null", a: {b: "hi"}},
            {_id: "a_double_array", a: [[]]},
            {_id: "a_empty_array", a: []},
            {_id: "a_object_array_no_b_nulls", a: [{b: 1}, {b: 3}, {b: "string"}]},
            {_id: "a_value_array_all_nulls", a: [null, null]},
            {_id: "a_value_array_no_nulls", a: [1, "string", 4]},
            {_id: "a_value_array_with_null", a: [1, "string", null, 4]},
            {_id: "a_value_array_with_undefined", a: [1, "string", undefined, 4]},
        ];

        assert(resultsEq(noProjectResults, expected), noProjectResults);

        const projectResults = coll.find(query, projectToOnlyA).toArray();
        assert(resultsEq(projectResults, extractAValues(expected)), projectResults);
    }());

    // Test the semantics of the query {a.b: {$nin: [null, <regex>]}}.
    (function testDottedNotInNullAndRegexQuery() {
        const query = {"a.b": {$nin: [null, /^str.*/]}};
        const noProjectResults = coll.find(query).toArray();
        const expected = [
            {_id: "a_subobject_b_not_null", a: {b: "hi"}},
            {_id: "a_double_array", a: [[]]},
            {_id: "a_empty_array", a: []},
            {_id: "a_value_array_all_nulls", a: [null, null]},
            {_id: "a_value_array_no_nulls", a: [1, "string", 4]},
            {_id: "a_value_array_with_null", a: [1, "string", null, 4]},
            {_id: "a_value_array_with_undefined", a: [1, "string", undefined, 4]},
        ];

        assert(resultsEq(noProjectResults, expected), noProjectResults);

        const projectResults = coll.find(query, projectToOnlyA).toArray();
        assert(resultsEq(projectResults, extractAValues(expected)), projectResults);
    }());

    // Test the results of similar dotted queries with an $elemMatch. These should have no
    // results since none of our documents have an array at the path "a.b".
    (function testDottedElemMatchValue() {
        let results = coll.find({"a.b": {$elemMatch: {$eq: null}}}).toArray();
        assert(resultsEq(results, []), tojson(results));

        results = coll.find({"a.b": {$elemMatch: {$ne: null}}}).toArray();
        assert(resultsEq(results, []), tojson(results));
    }());

    // Test null semantics within an $elemMatch object.
    (function testElemMatchObject() {
        // Test $elemMatch with equality to null.
        let noProjectResults = coll.find({a: {$elemMatch: {b: {$eq: null}}}}).toArray();
        const expectedEqualToNull = [
            {_id: "a_double_array", a: [[]]},
            {_id: "a_object_array_all_b_nulls", a: [{b: null}, {b: undefined}, {b: null}, {}]},
            {_id: "a_object_array_some_b_nulls", a: [{b: null}, {b: 3}, {b: null}]},
            {_id: "a_object_array_some_b_undefined", a: [{b: undefined}, {b: 3}]},
            {_id: "a_object_array_some_b_missing", a: [{b: 3}, {}]},
        ];
        assert(resultsEq(noProjectResults, expectedEqualToNull), tojson(noProjectResults));

        let projectResults =
            coll.find({a: {$elemMatch: {b: {$eq: null}}}}, projectToOnlyADotB).toArray();
        assert(resultsEq(projectResults,
                         [
                             {a: [[]]},
                             {a: [{b: null}, {b: undefined}, {b: null}, {}]},
                             {a: [{b: null}, {b: 3}, {b: null}]},
                             {a: [{b: undefined}, {b: 3}]},
                             {a: [{b: 3}, {}]},
                         ]),
               tojson(projectResults));

        // Test $elemMatch with not equal to null.
        noProjectResults = coll.find({a: {$elemMatch: {b: {$ne: null}}}}).toArray();
        const expectedNotEqualToNull = [
            {_id: "a_object_array_no_b_nulls", a: [{b: 1}, {b: 3}, {b: "string"}]},
            {_id: "a_object_array_some_b_nulls", a: [{b: null}, {b: 3}, {b: null}]},
            {_id: "a_object_array_some_b_undefined", a: [{b: undefined}, {b: 3}]},
            {_id: "a_object_array_some_b_missing", a: [{b: 3}, {}]},
        ];
        assert(resultsEq(noProjectResults, expectedNotEqualToNull), tojson(noProjectResults));

        projectResults =
            coll.find({a: {$elemMatch: {b: {$ne: null}}}}, projectToOnlyADotB).toArray();
        assert(resultsEq(projectResults,
                         [
                             {a: [{b: 1}, {b: 3}, {b: "string"}]},
                             {a: [{b: null}, {b: 3}, {b: null}]},
                             {a: [{b: undefined}, {b: 3}]},
                             {a: [{b: 3}, {}]},
                         ]),
               tojson(projectResults));
    }());
}

// Test without any indexes.
jsTestLog('Without any indexes');
const collNamePrefix = 'null_query_semantics_';
let collCount = 0;
let coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
testNullSemantics(coll);

const keyPatterns = [
    {keyPattern: {a: 1}},
    {keyPattern: {a: -1}},
    {keyPattern: {a: "hashed"}},
    {keyPattern: {a: 1}, options: {partialFilterExpression: {a: {$exists: true}}}},
    {keyPattern: {a: 1}, options: {sparse: true}},
    {keyPattern: {"a.b": 1}},
    {keyPattern: {_id: 1, "a.b": 1}},
    {keyPattern: {"a.b": 1, _id: 1}},
    {keyPattern: {"a.b": 1}, options: {partialFilterExpression: {a: {$exists: true}}}},
    {keyPattern: {"a.b": 1, _id: 1}, options: {sparse: true}},
    {keyPattern: {"$**": 1}},
    {keyPattern: {"a.$**": 1}}
];

// Test with a variety of other indexes.
for (let indexSpec of keyPatterns) {
    coll = db.getCollection(collNamePrefix + collCount++);
    coll.drop();
    jsTestLog(`Index spec: ${tojson(indexSpec)}`);
    assert.commandWorked(coll.createIndex(indexSpec.keyPattern, indexSpec.options));
    testNullSemantics(coll);
}

// Test that you cannot use a $ne: null predicate in a partial filter expression.
jsTestLog('Cannot use $ne: null predicate in a partial filter - 1');
coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandFailedWithCode(coll.createIndex({a: 1}, {partialFilterExpression: {a: {$ne: null}}}),
                             ErrorCodes.CannotCreateIndex);

jsTestLog('Cannot use $ne: null predicate in a partial filter - 2');
coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandFailedWithCode(
    coll.createIndex({a: 1}, {partialFilterExpression: {a: {$elemMatch: {$ne: null}}}}),
    ErrorCodes.CannotCreateIndex);
}());