summaryrefslogtreecommitdiff
path: root/jstests/aggregation
diff options
context:
space:
mode:
authorMihai Andrei <mihai.andrei@10gen.com>2021-08-06 02:25:20 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2021-08-06 02:50:20 +0000
commit76a5533ad21aef18a5e06689865a032a6185a3a1 (patch)
treeb81a525126484a97513791f5a9210b90d7839491 /jstests/aggregation
parent10e4898e0cd95e8b272ce8c098fb177a8971edf5 (diff)
downloadmongo-76a5533ad21aef18a5e06689865a032a6185a3a1.tar.gz
SERVER-57882 Add $minN and $maxN as expressions
Diffstat (limited to 'jstests/aggregation')
-rw-r--r--jstests/aggregation/expressions/n_expression.js64
1 files changed, 64 insertions, 0 deletions
diff --git a/jstests/aggregation/expressions/n_expression.js b/jstests/aggregation/expressions/n_expression.js
new file mode 100644
index 00000000000..c2f6857c356
--- /dev/null
+++ b/jstests/aggregation/expressions/n_expression.js
@@ -0,0 +1,64 @@
+/**
+ * Tests the 'n' family of accumulators implemented as expressions.
+ * TODO SERVER-57881: Add testcases for $firstN/$lastN.
+ */
+(function() {
+'use strict';
+
+const coll = db[jsTestName()];
+const doc = {
+ a: [1, 2, 3, 5, 7, 9],
+ n: 4,
+ diff: 2
+};
+
+const isExactTopNEnabled = db.adminCommand({getParameter: 1, featureFlagExactTopNAccumulator: 1})
+ .featureFlagExactTopNAccumulator.value;
+
+if (!isExactTopNEnabled) {
+ // Verify that $minN/$maxN cannot be used if the feature flag is set to false and ignore the
+ // rest of the test.
+ assert.commandFailedWithCode(coll.runCommand("aggregate", {
+ pipeline: [{$project: {output: {'$minN': {n: 3, output: [3, 1, 2, 3]}}}}],
+ cursor: {}
+ }),
+ 5787909);
+ return;
+}
+
+coll.drop();
+assert.commandWorked(coll.insert(doc));
+
+function testExpr(expression, expected) {
+ assert.eq(coll.aggregate([{$project: {_id: 0, output: expression}}]).toArray()[0].output,
+ expected);
+}
+
+let args = {n: 3, output: [5, 4, 3, 2, 1]};
+testExpr({$minN: args}, [1, 2, 3]);
+testExpr({$maxN: args}, [5, 4, 3]);
+args = {
+ n: 3,
+ output: [null, 2, null, 1]
+};
+testExpr({$minN: args}, [1, 2]);
+testExpr({$maxN: args}, [2, 1]);
+args = {
+ n: 3,
+ output: "$a"
+};
+testExpr({$minN: args}, [1, 2, 3]);
+testExpr({$maxN: args}, [9, 7, 5]);
+args = {
+ n: "$n",
+ output: "$a"
+};
+testExpr({$minN: args}, [1, 2, 3, 5]);
+testExpr({$maxN: args}, [9, 7, 5, 3]);
+args = {
+ n: {$subtract: ["$n", "$diff"]},
+ output: [3, 4, 5]
+};
+testExpr({$minN: args}, [3, 4]);
+testExpr({$maxN: args}, [5, 4]);
+})(); \ No newline at end of file