我需要加入3个收藏集。Teams,Schedule_Results,Stadiums。 我有数据文件 团队(TeamID,团队,大陆,联赛,人口) SCHEDULE_RESULTS(GameID,MatchType,MatchDate,SID,TeamID1,TeamID2,Team1_Score,Team2_Score) 体育场(SID,SName,SCity,SCapacity)
x = db.TEAMS.aggregate([
{"$lookup":
{
"from": "SCHEDULE_RESULTS",
"localField": "TeamID",
"foreignField": "TeamID1",
"as": "part1",
}
},
{"$lookup":
{
"from": "SCHEDULE_RESULTS",
"localField": "TeamID",
"foreignField": "TeamID2",
"as": "part2",
}
},
{
"$out":"PART1_COLLECTION"
}
])
z = db.PART1_COLLECTION.aggregate([
{"$lookup":
{
"from": "STADIUMS",
"localField": "SID",
"foreignField": "SID",
"as": "part3"
}
},
{
"$project":{
"_id":0,
"Team" : 1,
"TeamID" : 1,
"SID" :1,
"SName" : "$part3.SName",
"SCity" : "$part3.SCity"
}
},
{
"$out":"PART2_COLLECTION"
}
])
I need output as
{
"TeamName": "Russia",
"matchScores": [
{
"matchDate": "2018-06-14",
"stadiumName": "Luzhniki Stadium",
"cityName": "Moscow",
"teamName": "Russia",
"teamScore": 5,
"oppTeamName": "Saudi Arabia",
"oppTeamScore": 0
},
{
"matchDate": "2018-06-19",
"stadiumName": "Saint Petersburg Stadium",
"cityName": "Saint Petersburg",
"teamName": "Russia",
"teamScore": 3,
"oppTeamName": "Egypt",
"oppTeamScore": 1
},
{
"matchDate": "2018-06-25",
"stadiumName": "Samara Arena",
"cityName": "Samara",
"teamName": "Russia",
"teamScore": 0,
"oppTeamName": "Uruguay",
"oppTeamScore": 3
},
{
"matchDate": "2018-07-01",
"stadiumName": "Luzhniki Stadium",
"cityName": "Moscow",
"teamName": "Russia",
"teamScore": 1,
"oppTeamName": "Spain",
"oppTeamScore": 1
},
{
"matchDate": "2018-07-07",
"stadiumName": "Fisht Stadium",
"cityName": "Sochi",
"teamName": "Russia",
"teamScore": 2,
"oppTeamName": "Croatia",
"oppTeamScore": 2
}
]
}
for each Team.
Iam getting the output as
{
'_id':ObjectId('5d4468329fec4efe3e156817'),
'TeamID':'A1',
'Team':'Russia',
'Continent':'Europe',
'League':'UEFA',
'part1':[
{
'_id':ObjectId('5d4469179fec4efe3e156e02'),
'GameID':'G1',
'Groups':'A',
'MatchDate':'2018-06-14',
'SID':'S1',
'TeamID1':'A1',
'TeamID2':'A2',
'Team1_Score':5,
'Team2_Score':0
},
{
'_id':ObjectId('5d4469179fec4efe3e156e09'),
'GameID':'G17',
'Groups':'A',
'MatchDate':'2018-06-19',
'SID':'S2',
'TeamID1':'A1',
'TeamID2':'A3',
'Team1_Score':3,
'Team2_Score':1
},
{
'_id':ObjectId('5d4469179fec4efe3e156e35'),
'GameID':'G59',
'Groups':'Q',
'MatchDate':'2018-07-07',
'SID':'S3',
'TeamID1':'A1',
'TeamID2':'D3',
'Team1_Score':2,
'Team2_Score':2
}
],
'part2':[
{
'_id':ObjectId('5d4469179fec4efe3e156e1b'),
'GameID':'G33',
'Groups':'A',
'MatchDate':'2018-06-25',
'SID':'S8',
'TeamID1':'A4',
'TeamID2':'A1',
'Team1_Score':3,
'Team2_Score':0
},
{
'_id':ObjectId('5d4469179fec4efe3e156e2d'),
'GameID':'G51',
'Groups':'X',
'MatchDate':'2018-07-01',
'SID':'S1',
'TeamID1':'B2',
'TeamID2':'A1',
'Team1_Score':1,
'Team2_Score':1
}
],
'part3':[
]
}
我是MongoDB的新手。任何人都可以请帮我在这
答案 0 :(得分:0)
请尝试:
db.getCollection('TEAM').aggregate([
{
$lookup: {
from: "SCHEDULE_RESULTS",
let: { teamID: "$TeamID" },
pipeline: [
{
$match:
{
$expr:
{
$or:
[
{ $eq: ["$TeamID1", "$$teamID"] },
{ $eq: ["$TeamID2", "$$teamID"] }
]
}
}
}
],
as: "team_StadiumData"
}
}, { $unwind: { path: '$team_StadiumData' } },
{
$lookup: {
from: "STADIUM",
let: { stadiumID: "$team_StadiumData.SID" },
pipeline: [
{
$match:
{
$expr:
{
$eq: ["$SID", "$$stadiumID"]
}
}
}
],
as: "finalData"
}
},
{ $addFields: { 'team_StadiumData.teamName':'$Team', 'team_StadiumData.stadiumName': { $arrayElemAt: ["$finalData.SName", 0] }, 'team_StadiumData.stadiumCity': { $arrayElemAt: ["$finalData.SCity", 0] }} },
{ $group: { _id: '$TeamID', team_StadiumData: { $push: '$team_StadiumData' }, docs: { $first: '$$ROOT' } } },
{ $addFields: { 'docs.team_StadiumData': '$team_StadiumData' } },
{ $replaceRoot: { newRoot: '$docs' } },
{ $project: { team_StadiumData: 1,Team: 1, _id: 0 } }
])
如果您需要来自 TEAM 的文档,而该文档在 SCHEDULE_RESULTS 中没有匹配的记录,那么您需要添加一个选项:preserveNullAndEmptyArrays: true
$unwind
第一个阶段的$lookup
。