summaryrefslogtreecommitdiff
path: root/jstests/aggregation/bugs/groupMissing.js
blob: c08e70185b154e54cdfbe4182712c18749686346 (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
// $group has inconsistent behavior when differentiating between null and missing values, provided
// this test passes. Here, we check the cases where it is correct, and those where it is currently
// incorrect.
//
// This test issues some pipelines where it assumes an initial $sort will be absorbed and be
// covered, which will not happen if the $sort is within a $facet stage.
// @tags: [do_not_wrap_aggregations_in_facets]
load('jstests/aggregation/extras/utils.js');  // For resultsEq.

(function() {
    "use strict";

    var coll = db.groupMissing;
    coll.drop();

    coll.insert({a: null});
    coll.insert({});

    var res = coll.aggregate({$group: {_id: "$a"}});
    var arr = res.toArray();
    assert.eq(arr.length, 1);
    assert.eq(arr[0]._id, null);

    coll.createIndex({a: 1});
    res = coll.aggregate({$sort: {a: 1}}, {$group: {_id: "$a"}});
    arr = res.toArray();
    assert.eq(arr.length, 1);
    assert.eq(arr[0]._id, null);

    coll.drop();

    coll.insert({a: null});
    coll.insert({});

    // Bug, see SERVER-21992.
    res = coll.aggregate({$group: {_id: {a: "$a"}}});
    assert(resultsEq(res.toArray(), [{_id: {a: null}}]));

    // Correct behavior after SERVER-21992 is fixed.
    if (0) {
        res = coll.aggregate({$group: {_id: {a: "$a"}}});
        assert(resultsEq(res.toArray(), [{_id: {a: null}}, {_id: {a: {}}}]));
    }

    // Bug, see SERVER-21992.
    coll.createIndex({a: 1});
    res = coll.aggregate({$group: {_id: {a: "$a"}}});
    assert(resultsEq(res.toArray(), [{_id: {a: null}}]));

    // Correct behavior after SERVER-21992 is fixed.
    if (0) {
        res = coll.aggregate({$group: {_id: {a: "$a"}}});
        assert(resultsEq(res.toArray(), [{_id: {a: null}}, {_id: {a: {}}}]));
    }

    coll.drop();
    coll.insert({a: null, b: 1});
    coll.insert({b: 1});
    coll.insert({a: null, b: 1});

    res = coll.aggregate({$group: {_id: {a: "$a", b: "$b"}}});
    assert(resultsEq(res.toArray(), [{_id: {b: 1}}, {_id: {a: null, b: 1}}]));

    // Bug, see SERVER-23229.
    coll.createIndex({a: 1, b: 1});
    res = coll.aggregate({$sort: {a: 1, b: 1}}, {$group: {_id: {a: "$a", b: "$b"}}});
    assert(resultsEq(res.toArray(), [{_id: {a: null, b: 1}}]));

    // Correct behavior after SERVER-23229 is fixed.
    if (0) {
        coll.createIndex({a: 1, b: 1});
        res = coll.aggregate({$sort: {a: 1, b: 1}}, {$group: {_id: {a: "$a", b: "$b"}}});
        assert(resultsEq(res.toArray(), [{_id: {b: 1}}, {_id: {a: null, b: 1}}]));
    }
}());