summaryrefslogtreecommitdiff
path: root/jstests/aggregation/bugs/reverseArray.js
blob: 0fa4010654b1ad58c1a5b5d3b1baaccb3ca1a734 (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
// SERVER-23029 added a new expression, $reverseArray, which consumes an array or a nullish value
// and produces either the reversed version of that array, or null. In this test file, we check the
// behavior and error cases.
load("jstests/aggregation/extras/utils.js");  // For assertErrorCode.

(function() {
    "use strict";

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

    // We need a document to flow through the pipeline, even though we don't care what fields it
    // has.
    coll.insert({});

    assertErrorCode(coll, [{$project: {reversed: {$reverseArray: 1}}}], 34435);

    var res = coll.aggregate([{$project: {reversed: {$reverseArray: {$literal: [1, 2]}}}}]);
    var output = res.toArray();
    assert.eq(1, output.length);
    assert.eq(output[0].reversed, [2, 1]);

    var res = coll.aggregate([{$project: {reversed: {$reverseArray: {$literal: [[1, 2]]}}}}]);
    var output = res.toArray();
    assert.eq(1, output.length);
    assert.eq(output[0].reversed, [[1, 2]]);

    var res = coll.aggregate([{$project: {reversed: {$reverseArray: "$notAField"}}}]);
    var output = res.toArray();
    assert.eq(1, output.length);
    assert.eq(output[0].reversed, null);
}());