summaryrefslogtreecommitdiff
path: root/jstests/views
diff options
context:
space:
mode:
authorMax Hirschhorn <max.hirschhorn@mongodb.com>2016-08-10 20:20:19 -0400
committerMax Hirschhorn <max.hirschhorn@mongodb.com>2016-08-10 20:20:19 -0400
commitfad11e0917e79e5cfa2bb744e9ec5d1bcb97a608 (patch)
tree32eba6f1cd9558d7f0aa2fe07a4135b9f6cbd445 /jstests/views
parenta0b7e4fc8cf224505267b2fe589975ba36f49078 (diff)
downloadmongo-fad11e0917e79e5cfa2bb744e9ec5d1bcb97a608.tar.gz
SERVER-24769 Add support for $lookup and $graphLookup on a view.
Diffstat (limited to 'jstests/views')
-rw-r--r--jstests/views/views_aggregation.js94
1 files changed, 93 insertions, 1 deletions
diff --git a/jstests/views/views_aggregation.js b/jstests/views/views_aggregation.js
index a6281f5bc94..87cf3d7908e 100644
--- a/jstests/views/views_aggregation.js
+++ b/jstests/views/views_aggregation.js
@@ -2,7 +2,7 @@
(function() {
"use strict";
- // For arrayEq and orderedArrayEq.
+ // For arrayEq, assertErrorCode, and orderedArrayEq.
load("jstests/aggregation/extras/utils.js");
let viewsDB = db.getSiblingDB("views_aggregation");
@@ -68,4 +68,96 @@
const doOrderedSort = true;
assertAggResultEq("popSortedView", [], allDocuments.sort(byPopulation), doOrderedSort);
assertAggResultEq("popSortedView", [{$limit: 1}, {$project: {_id: 1}}], [{_id: "Palo Alto"}]);
+
+ // Test that the $out stage errors when given a view namespace.
+ assertErrorCode(coll, [{$out: "emptyPipelineView"}], 18631);
+
+ // Test that the $lookup stage resolves the view namespace referenced in the 'from' field.
+ assertAggResultEq(
+ coll.getName(),
+ [
+ {$match: {_id: "New York"}},
+ {$lookup: {from: "identityView", localField: "_id", foreignField: "_id", as: "matched"}},
+ {$unwind: "$matched"},
+ {$project: {_id: 1, matchedId: "$matched._id"}}
+ ],
+ [{_id: "New York", matchedId: "New York"}]);
+
+ // Test that the $graphLookup stage resolves the view namespace referenced in the 'from' field.
+ assertAggResultEq(coll.getName(),
+ [
+ {$match: {_id: "New York"}},
+ {
+ $graphLookup: {
+ from: "identityView",
+ startWith: "$_id",
+ connectFromField: "_id",
+ connectToField: "_id",
+ as: "matched"
+ }
+ },
+ {$unwind: "$matched"},
+ {$project: {_id: 1, matchedId: "$matched._id"}}
+ ],
+ [{_id: "New York", matchedId: "New York"}]);
+
+ // Test that the $lookup stage resolves the view namespace referenced in the 'from' field of
+ // another $lookup stage nested inside of it.
+ assert.commandWorked(viewsDB.runCommand({
+ create: "viewWithLookupInside",
+ viewOn: coll.getName(),
+ pipeline: [
+ {
+ $lookup:
+ {from: "identityView", localField: "_id", foreignField: "_id", as: "matched"}
+ },
+ {$unwind: "$matched"},
+ {$project: {_id: 1, matchedId: "$matched._id"}}
+ ]
+ }));
+
+ assertAggResultEq(
+ coll.getName(),
+ [
+ {$match: {_id: "New York"}},
+ {
+ $lookup: {
+ from: "viewWithLookupInside",
+ localField: "_id",
+ foreignField: "matchedId",
+ as: "matched"
+ }
+ },
+ {$unwind: "$matched"},
+ {$project: {_id: 1, matchedId1: "$matched._id", matchedId2: "$matched.matchedId"}}
+ ],
+ [{_id: "New York", matchedId1: "New York", matchedId2: "New York"}]);
+
+ // Test that the $graphLookup stage resolves the view namespace referenced in the 'from' field
+ // of a $lookup stage nested inside of it.
+ let graphLookupPipeline = [
+ {$match: {_id: "New York"}},
+ {
+ $graphLookup: {
+ from: "viewWithLookupInside",
+ startWith: "$_id",
+ connectFromField: "_id",
+ connectToField: "matchedId",
+ as: "matched"
+ }
+ },
+ {$unwind: "$matched"},
+ {$project: {_id: 1, matchedId1: "$matched._id", matchedId2: "$matched.matchedId"}}
+ ];
+
+ assertAggResultEq(coll.getName(),
+ graphLookupPipeline,
+ [{_id: "New York", matchedId1: "New York", matchedId2: "New York"}]);
+
+ // Test that the $facet stage resolves the view namespace referenced in the 'from' field of a
+ // $lookup stage nested inside of a $graphLookup stage.
+ assertAggResultEq(
+ coll.getName(),
+ [{$facet: {nested: graphLookupPipeline}}],
+ [{nested: [{_id: "New York", matchedId1: "New York", matchedId2: "New York"}]}]);
}());