mongodb多表查询

Posted atttitude

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mongodb多表查询相关的知识,希望对你有一定的参考价值。

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":"English",
"Score":75
}]
},
{
"StudentID":"S000002",
"TestID":"T000001",
"Scores":[{
"Course":"Chinese",
"Score":70
},{
"Course":"Maths",
"Score":80
},{
"Course":"English",
"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多表查询的主要内容,如果未能解决你的问题,请参考以下文章

mongodb 多表分页怎么查询

Django怎么多表联合查询

MYSQL 多表联合查询问题

MongoDB多表关联分组查询指定行数数据实践遇坑记及解析

MongoDB多表关联分组查询指定行数数据实践遇坑记及解析

mongodb怎么查询多个关键字?