我正在尝试学习mongodb并想知道它是否支持多个字段的内连接。
我期待的是mongodb查询相当于:
select * from tablea A inner join tableb B on a.id = b.id and a.name = b.name
我尝试使用$ match但我无法看到任何结果。 这些是我的mongodb集合: CountryState:
{
"countryCode" : "1",
"stateCode" : "1"
}
{
"countryCode" : "1",
"stateCode" : "3"
}
CountryStateLookup:
{
"countryCode" : "1",
"stateCode" : "2",
"countryName" : "TamilNadu"
}
{
"countryCode" : "1",
"stateCode" : "1",
"countryName" : "Kerala"
}
{
"countryCode" : "2",
"stateCode" : "1",
"countryName" : "Karnataka"
}
{
"countryCode" : "2",
"stateCode" : "2",
"countryName" : "California"
}
预期产出:
{
"countryCode" : "1",
"stateCode" : "1",
"countryName" : "Kerala"
}
这是我尝试的以下查询:(不知道如何使用$ match运算符)
db.countryState.aggregate([
{
$lookup:{
from:"countryStateLookup",
localField:"stateCode",
foreignField:"stateCode",
as:"stateCodeName"
}
},
{
$unwind:"$stateCodeName"
},
{
$lookup:{
from:"countryStateLookup",
localField:"countryCode",
foreignField:"countryCode",
as:"countryCodeName"
}
},
{
$unwind:"$countryCodeName"
},
{
$match:{
"countryCode":"$stateCodeName.countryCode",
"stateCode":"$stateCodeName.stateCode"
}
}
])
任何帮助将不胜感激,谢谢!