summaryrefslogtreecommitdiff
path: root/jstests/aggregation
diff options
context:
space:
mode:
authorBrigitte Lamarche <thesisiwbl@gmail.com>2018-12-20 14:24:02 -0500
committerBrigitte Lamarche <thesisiwbl@gmail.com>2019-01-03 15:13:57 -0500
commitbb9114dc71bfcf42422471f7789eca00881b8864 (patch)
tree4809839aab0c3f97bf56a66df15cd8eae8b03ca2 /jstests/aggregation
parent68832e1f70fa4571673ab337ae2e529b04b67e6b (diff)
downloadmongo-bb9114dc71bfcf42422471f7789eca00881b8864.tar.gz
SERVER-38362 Prevent $out stage within $lookup
Diffstat (limited to 'jstests/aggregation')
-rw-r--r--jstests/aggregation/sources/out/out_in_lookup_not_allowed.js72
1 files changed, 72 insertions, 0 deletions
diff --git a/jstests/aggregation/sources/out/out_in_lookup_not_allowed.js b/jstests/aggregation/sources/out/out_in_lookup_not_allowed.js
new file mode 100644
index 00000000000..e709fbce1d3
--- /dev/null
+++ b/jstests/aggregation/sources/out/out_in_lookup_not_allowed.js
@@ -0,0 +1,72 @@
+// Tests that $out cannot be used within a $lookup pipeline.
+(function() {
+ "use strict";
+
+ load("jstests/aggregation/extras/utils.js"); // For assertErrorCode.
+ load("jstests/libs/collection_drop_recreate.js"); // For assertDropCollection.
+
+ const ERROR_CODE_OUT_BANNED_IN_LOOKUP = 51047;
+ const ERROR_CODE_OUT_LAST_STAGE_ONLY = 40601;
+ const coll = db.out_in_lookup_not_allowed;
+ coll.drop();
+
+ const from = db.out_in_lookup_not_allowed_from;
+ from.drop();
+
+ let pipeline = [
+ {
+ $lookup: {
+ pipeline: [{$out: "out_collection"}],
+ from: from.getName(),
+ as: "c",
+ }
+ },
+ ];
+
+ assertErrorCode(coll, pipeline, ERROR_CODE_OUT_BANNED_IN_LOOKUP);
+
+ pipeline = [
+ {
+ $lookup: {
+ pipeline: [{$project: {x: 0}}, {$out: "out_collection"}],
+ from: from.getName(),
+ as: "c",
+ }
+ },
+ ];
+
+ assertErrorCode(coll, pipeline, ERROR_CODE_OUT_BANNED_IN_LOOKUP);
+
+ pipeline = [
+ {
+ $lookup: {
+ pipeline: [{$out: "out_collection"}, {$match: {x: true}}],
+ from: from.getName(),
+ as: "c",
+ }
+ },
+ ];
+
+ // Pipeline will fail because $out is not last in the subpipeline.
+ // Validation for $out in a $lookup's subpipeline occurs at a later point.
+ assertErrorCode(coll, pipeline, ERROR_CODE_OUT_LAST_STAGE_ONLY);
+
+ // Create view which contains $out within $lookup.
+ assertDropCollection(coll.getDB(), "view1");
+
+ pipeline = [
+ {
+ $lookup: {
+ pipeline: [{$out: "out_collection"}],
+ from: from.getName(),
+ as: "c",
+ }
+ },
+ ];
+
+ // Pipeline will fail because $out is not allowed to exist within a $lookup.
+ // Validation for $out in a view occurs at a later point.
+ const cmdRes =
+ coll.getDB().runCommand({create: "view1", viewOn: coll.getName(), pipeline: pipeline});
+ assert.commandFailedWithCode(cmdRes, ERROR_CODE_OUT_BANNED_IN_LOOKUP);
+}());