summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTed Tuckman <ted.tuckman@mongodb.com>2021-11-01 12:12:33 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2021-11-01 12:35:22 +0000
commitf7fdc731424d93367129f5d91064dfd31300f883 (patch)
treef312f6c8eba09215cb39adc38be81677f1365e9f
parent8ec2e9cefaaa92e83d5a0cecd7c7d947eca32f4e (diff)
downloadmongo-f7fdc731424d93367129f5d91064dfd31300f883.tar.gz
SERVER-61034 Don't always add extra match to $graphLookup
-rw-r--r--src/mongo/db/pipeline/document_source_graph_lookup.cpp13
1 files changed, 10 insertions, 3 deletions
diff --git a/src/mongo/db/pipeline/document_source_graph_lookup.cpp b/src/mongo/db/pipeline/document_source_graph_lookup.cpp
index 60e74b9c043..c126ea0977e 100644
--- a/src/mongo/db/pipeline/document_source_graph_lookup.cpp
+++ b/src/mongo/db/pipeline/document_source_graph_lookup.cpp
@@ -406,6 +406,7 @@ boost::optional<BSONObj> DocumentSourceGraphLookUp::makeMatchStageFromFrontier(
// present and eliminate documents that would match the others later.
bool matchNull = false;
bool matchUndefined = false;
+ bool seenMissing = false;
BSONObjBuilder match;
{
BSONObjBuilder query(match.subobjStart("$match"));
@@ -426,16 +427,22 @@ boost::optional<BSONObj> DocumentSourceGraphLookUp::makeMatchStageFromFrontier(
matchNull = true;
} else if (value.getType() == BSONType::Undefined) {
matchUndefined = true;
+ } else if (value.missing()) {
+ seenMissing = true;
}
in << value;
}
}
}
}
- // We never want to see documents where the 'connectToField' is missing.
- auto existsMatch = BSON(_connectToField.fullPath() << BSON("$exists" << true));
- andObj << existsMatch;
+ // We never want to see documents where the 'connectToField' is missing. Only add a
+ // check for it in situations where we might match it accidentally.
+ if (matchNull || matchUndefined || seenMissing) {
+ auto existsMatch = BSON(_connectToField.fullPath() << BSON("$exists" << true));
+ andObj << existsMatch;
+ }
// If matching null or undefined, make sure we don't match the other one.
+ // If seenMissing is true, we've already filtered out missing values above.
if (matchNull || matchUndefined) {
if (!matchUndefined) {
auto notUndefined =