summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Wahlin <james@mongodb.com>2020-05-01 13:54:53 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-05-01 21:24:37 +0000
commit265c4da7b3a9b97d40054e9d4da8ad75c4aca8a0 (patch)
tree9fc3d5929da1953639cf5cc86127678c1d6f2cba
parent919cc73d751111856c048d82eebd110f2c4b0c60 (diff)
downloadmongo-265c4da7b3a9b97d40054e9d4da8ad75c4aca8a0.tar.gz
SERVER-47765 Make variables declared above DocumentSourceGraphLookup available within
-rw-r--r--jstests/aggregation/sources/graphLookup/filter.js24
-rw-r--r--src/mongo/db/pipeline/document_source_graph_lookup.cpp4
2 files changed, 28 insertions, 0 deletions
diff --git a/jstests/aggregation/sources/graphLookup/filter.js b/jstests/aggregation/sources/graphLookup/filter.js
index 69027500aae..ce1af67bd08 100644
--- a/jstests/aggregation/sources/graphLookup/filter.js
+++ b/jstests/aggregation/sources/graphLookup/filter.js
@@ -91,4 +91,28 @@
.toArray()[0];
assert.eq(res.results.length, 1);
+
+ // $expr within `restrictSearchWithMatch` has access to variables declared at a higher level.
+ res = local
+ .aggregate([{
+ $lookup: {
+ from: "local",
+ let : {foo: true},
+ pipeline: [{
+ $graphLookup: {
+ from: "foreign",
+ startWith: "$starting",
+ connectFromField: "to",
+ connectToField: "from",
+ as: "results",
+ restrictSearchWithMatch:
+ {$expr: {$eq: ["$shouldBeIncluded", "$$foo"]}}
+ }
+ }],
+ as: "array"
+ }
+ }])
+ .toArray()[0];
+
+ assert.eq(res.array[0].results.length, 1);
})();
diff --git a/src/mongo/db/pipeline/document_source_graph_lookup.cpp b/src/mongo/db/pipeline/document_source_graph_lookup.cpp
index 6e9c4776df4..bc055a679e3 100644
--- a/src/mongo/db/pipeline/document_source_graph_lookup.cpp
+++ b/src/mongo/db/pipeline/document_source_graph_lookup.cpp
@@ -482,6 +482,10 @@ DocumentSourceGraphLookUp::DocumentSourceGraphLookUp(
boost::none,
expCtx->getCollator() ? expCtx->getCollator()->clone() : nullptr);
+ _fromExpCtx->variables = expCtx->variables;
+ _fromExpCtx->variablesParseState =
+ expCtx->variablesParseState.copyWith(expCtx->variables.useIdGenerator());
+
// We append an additional BSONObj to '_fromPipeline' as a placeholder for the $match stage
// we'll eventually construct from the input document.
_fromPipeline = resolvedNamespace.pipeline;