Db. Classmembers.insertmany ([
{
"ClassID": "CL0001",
"StudentID": "S000001",
"Datefrom": "2015-09-01"
},
{
"ClassID": "CL0002",
"StudentID": "S000002",
"Datefrom": "2015-09-01"
}
]);
Db. Scores.insertmany ([
{
"StudentID": "S000001",
"TestID": "T000001",
"Scores": [{
"Course": "Chinese",
"Score": 80
},{
"Course": "Maths",
"Score": 70
},{
"Course": "中文版",
"Score": 75
}]
},
{
"StudentID": "S000002",
"TestID": "T000001",
"Scores": [{
"Course": "Chinese",
"Score": 70
},{
"Course": "Maths",
"Score": 80
},{
"Course": "中文版",
"Score": 82
}]
}
]);
Db. Classmembers.aggregate ([
{
$match: {ClassID: ' CL0001 '}
},
{
$lookup:
{
From: "Scores",
Localfield: "StudentID",
Foreignfield: "StudentID",
As: "Scoresdocs"
}
},
{$match: {"Scoresdocs.testid": ' T000001 '}},
{$project: {_id:0, classid:1, scoresdocs:1}},
{$unwind: ' $ScoresDocs '},
{$unwind: ' $ScoresDocs. Scores '},
{$match: {' ScoresDocs.Scores.Course ': ' Chinese '}}
])
MongoDB Multi-Table query