summaryrefslogtreecommitdiff
path: root/jstests/aggregation/expressions/expression_function.js
blob: 27b38bc9148e2bc87141beb3d6a7b320ee6023d6 (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
// Tests basic functionality of the $function expression.
(function() {
"use strict";

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

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

function f_finalize(first, second) {
    return first + second;
}

for (let i = 0; i < 5; i++) {
    assert.commandWorked(coll.insert({value: i}));
}

let pipeline = [{
    $project: {
        newValue: {
            $function: {
                args: ["$value", -1],
                body: f_finalize,
                lang: "js",
            },
        },
        _id: 0,
    }
}];

let results = coll.aggregate(pipeline, {cursor: {batchSize: 1}}).toArray();
assert(resultsEq(results,
                 [{newValue: -1}, {newValue: 0}, {newValue: 1}, {newValue: 2}, {newValue: 3}]),
       results);

// Test that the 'body' function accepts a string argument.
pipeline[0].$project.newValue.$function.body = f_finalize.toString();
results = coll.aggregate(pipeline, {cursor: {}}).toArray();
assert(resultsEq(results,
                 [{newValue: -1}, {newValue: 0}, {newValue: 1}, {newValue: 2}, {newValue: 3}]),
       results);

// Test that function can take an expression that evaluates to an array for the 'args' parameter.
coll.drop();
for (let i = 0; i < 5; i++) {
    assert.commandWorked(coll.insert({values: [i, i * 2]}));
}
pipeline = [{
    $project: {
        newValue: {
            $function: {
                args: "$values",
                body: f_finalize,
                lang: "js",
            },
        },
        _id: 0,
    }
}];

results = coll.aggregate(pipeline, {cursor: {}}).toArray();
assert(resultsEq(results,
                 [{newValue: 0}, {newValue: 3}, {newValue: 6}, {newValue: 9}, {newValue: 12}]),
       results);

// Test that the command correctly fails for invalid arguments.
pipeline = [{
    $project: {
        newValue: {
            $function: {
                'args': 'must evaluate to an array',
                'body': f_finalize,
                'lang': 'js',
            },
        },
        _id: 0,
    }
}];
assert.commandFailedWithCode(
    db.runCommand({aggregate: coll.getName(), pipeline: pipeline, cursor: {}}), 31266);

pipeline = [{
    $project: {
        newValue: {
            $function: {
                'args': [1, 3],
                'body': 'this is not a valid function!',
                'lang': 'js',
            },
        },
        _id: 0,
    }
}];
assert.commandFailedWithCode(
    db.runCommand({aggregate: coll.getName(), pipeline: pipeline, cursor: {}}),
    ErrorCodes.JSInterpreterFailure);

pipeline = [{
    $project: {
        newValue: {
            $function: {
                'args': [1, 3],
                'body': f_finalize,
            },
        },
        _id: 0,
    }
}];
assert.commandFailedWithCode(
    db.runCommand({aggregate: coll.getName(), pipeline: pipeline, cursor: {}}), 31418);

pipeline = [{
    $project: {
        newValue: {
            $function: {
                'args': [1, 3],
                'body': f_finalize,
                'lang': 'not js!',
            },
        },
        _id: 0,
    }
}];
assert.commandFailedWithCode(
    db.runCommand({aggregate: coll.getName(), pipeline: pipeline, cursor: {}}), 31419);

// Test that we fail if the 'args' field is not an array.
pipeline = [{
    $project: {
        newValue: {
            $function: {
                'args': "A string!",
                'body': f_finalize,
                'lang': 'js',
            },
        },
        _id: 0,
    }
}];
assert.commandFailedWithCode(
    db.runCommand({aggregate: coll.getName(), pipeline: pipeline, cursor: {}}), 31266);
})();