With right combination of $lookup, $project and $match, you can join mutiple tables on parameters. This is because they can be chained multiple times.
Step 1: Link all tables
$lookup - one for each table in query
$unwind - because data is denormalised correctly, else wrapped in arrays
Python code..
db.LeftTable.aggregate([
# connect all tables
{"$lookup": {
"from": "RightTable",
"localField": "ID",
"foreignField": "ID",
"as": "R"
}},
{"$unwind": "R"}
# {"$lookup": {
# "from": "TopTable",
# "localField": "ID",
# "foreignField": "ID",
# "as": "T"
# }},
# {"$unwind": "T"},
])
Step 2: Define all conditionals
$project : define all conditional statements here, plus all the variables you'd like to select.
Python Code..
db.LeftTable.aggregate([
# connect all tables
{"$lookup": {
"from": "RightTable",
"localField": "ID",
"foreignField": "ID",
"as": "R"
}},
{"$unwind": "R"},
# {"$lookup": {
# "from": "TopTable",
# "localField": "ID",
# "foreignField": "ID",
# "as": "T"
# }},
# {"$unwind": "T"},
# define conditionals + variables
{"$project": {
"midEq": {"$eq": ["$MID", "$R.MID"]},
# "midGt": {"$gt": ["$MID", "$T.MID"]},
"ID": 1, "MOB": 1, "MID": 1
}}
])
Step 3: Join all the conditionals
$match - join all conditions using OR or AND etc. There can be multiples of these.
$project: undefine all conditionals
Python Code..
db.LeftTable.aggregate([
# connect all tables
{"$lookup": {
"from": "RightTable",
"localField": "ID",
"foreignField": "ID",
"as": "R"
}},
{"$unwind": "$R"},
# {"$lookup": {
# "from": "TopTable",
# "localField": "ID",
# "foreignField": "ID",
# "as": "T"
#}},
#{"$unwind": "$T"},
# define conditionals + variables
{"$project": {
"midEq": {"$eq": ["$MID", "$R.MID"]},
# "midGt": {"$gt": ["$MID", "$T.MID"]},
"ID": 1, "MOB": 1, "MID": 1
}},
# join all conditionals
{"$match": {
"$and": [
{"R.TIM": {"$gt": 0}},
{"MOB": {"$exists": True}},
{"midEq": {"$eq": True}},]
}},
# undefine conditionals
{"$project": {
"midEq": 0,
# "midGt": 0
}}
])
Pretty much any combination of tables, conditionals and joins can be done in this manner.