From 265c4da7b3a9b97d40054e9d4da8ad75c4aca8a0 Mon Sep 17 00:00:00 2001 From: James Wahlin Date: Fri, 1 May 2020 13:54:53 -0400 Subject: SERVER-47765 Make variables declared above DocumentSourceGraphLookup available within --- jstests/aggregation/sources/graphLookup/filter.js | 24 ++++++++++++++++++++++ .../db/pipeline/document_source_graph_lookup.cpp | 4 ++++ 2 files changed, 28 insertions(+) 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; -- cgit v1.2.1