I’m trying to create a MongoDB pipeline that does the same as this SQL query:
SELECT DISTINCT A.ID FROM
(Query_A) A
INNER JOIN
(Query_B) B
ON A.ID = B.ID
I’ve come up with the following code to run both query pipelines:
[
{
"$facet": {
"query_a": [...],
"query_b": [...]
}
},
...
]
These pipelines return IDs and I want to get an intersection of these IDs ("$query_a.ID" equal to “$query_b.ID”).
EDIT:
The result of the $facet step is:
[
{
queryA: [
{"ID": "c80ea2cb-3272-77ae-8f46-d95de600c5bf",
"date": "1"},
{"ID": "cdbcc129-548a-9d51-895a-1538200664e6",
"date": "2"},
{"ID": "a4ece1ba-42ae-e735-17b0-f619daa506f9",
"date": "3"}
],
queryB: [
{"ID": "c80ea2cb-3272-77ae-8f46-d95de600c5bg",
"date": "4"},
{"ID": "cdbcc129-548a-9d51-895a-1538200664e6",
"date": "5"},
{"ID": "a4ece1ba-42ae-e735-17b0-f619daa506f9",
"date": "6"}
]
}
]
But my requested result is:
[
{
"dateA": "2",
"dateB": "5",
"intersection": "cdbcc129-548a-9d51-895a-1538200664e6"
},
{
"dateA": "3",
"dateB": "6",
"intersection": "a4ece1ba-42ae-e735-17b0-f619daa506f9"
}
]