summaryrefslogtreecommitdiff
path: root/jstests/core/timeseries/timeseries_groupby_reorder.js
blob: c4c34bc12492e7d223e978e256b8f0130fa83eb5 (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
/**
 * Test the behavior of $group on time-series collections.
 *
 * @tags: [
 *   directly_against_shardsvrs_incompatible,
 *   does_not_support_stepdowns,
 *   does_not_support_transactions,
 *   requires_fcv_61,
 * ]
 */
(function() {
"use strict";

load("jstests/libs/fixture_helpers.js");
load("jstests/core/timeseries/libs/timeseries.js");

const coll = db.timeseries_groupby_reorder;
coll.drop();

assert.commandWorked(
    db.createCollection(coll.getName(), {timeseries: {metaField: "meta", timeField: "t"}}));

const t = new Date();
assert.commandWorked(coll.insert({_id: 0, t: t, b: 1, c: 1}));
assert.commandWorked(coll.insert({_id: 0, t: t, b: 2, c: 2}));
assert.commandWorked(coll.insert({_id: 0, t: t, b: 3, c: 3}));

// Test reordering the groupby and internal unpack buckets.
if (!isMongos(db)) {
    const res = coll.explain("queryPlanner").aggregate([
        {$group: {_id: '$meta', accmin: {$min: '$b'}, accmax: {$max: '$c'}}}
    ]);

    assert.docEq({
        "$group":
            {_id: "$meta", accmin: {"$min": "$control.min.b"}, accmax: {"$max": "$control.max.c"}}
    },
                 res.stages[1]);
}

let res = coll.aggregate([{$group: {_id: '$meta', accmin: {$min: '$b'}, accmax: {$max: '$c'}}}])
              .toArray();
assert.docEq([{"_id": null, "accmin": 1, "accmax": 3}], res);

// Test SERVER-73822 fix: complex $min and $max (i.e. not just straight field refs) work correctly.
res = coll.aggregate([{
              $group: {
                  _id: '$meta',
                  accmin: {$min: {$add: ["$b", "$c"]}},
                  accmax: {$max: {$add: ["$b", "$c"]}}
              }
          }])
          .toArray();
assert.docEq([{"_id": null, "accmin": 2, "accmax": 6}], res);
})();