示例用例:学生正在学习a,以获得需要完成五门课程的证书。这些课可以按任何顺序上。在前端,我想建立一个完整的信息仪表板。仪表板将显示所提供的所有课程,并将学生的表现集成到显示器中。我正在寻找一个单一的数组来构建仪表板。
我有一个mongodb集合('classlist)显示所有五门课程。
[
{
class_longname: "Underwater Volleyball",
class_shortname: "uv",
difficulty_1_10: 5,
instructor_name: "Mr. Smith",
special_needs: "snorkel and mask",
duration: "20 hours"
},
{
class_longname: "Yoga in the Mountains",
class_shortname: "ym",
difficulty_1_10: 7,
instructor_name: "Ms. Walrus",
special_needs: "yoga mat",
duration: "12 hours"
},
{
class_longname: "Roller skating disco dance party",
class_shortname: "rs",
difficulty_1_10: 4,
instructor_name: "Ms. Pineapple",
special_needs: "roller skates",
duration: "10 hours"
},
{
class_longname: "MongoDB database programming for beginners",
class_shortname: "mb",
difficulty_1_10: 4,
instructor_name: "Mr. Smith",
special_needs: "laptop",
duration: "4 hours"
},
{
class_longname: "Learn to Ride a Unicycle",
class_shortname: "lu",
difficulty_1_10: 7,
instructor_name: "Ms. Apple",
special_needs: "bring your own unicycle",
duration: "15 hours"
}
]
我有一个MongoDb集合(course_progress)为所有学生:
[
{
student_name: "Tom R.",
class_shortname: "ym",
date_completed: ...,
pass: true,
},
{
student_name: "Tom R.",
class_shortname: "uv",
date_completed: ...,
pass: true,
},
{
student_name: "Betty S.",
class_shortname: "mb",
date_completed: ...,
pass: true,
},
{
student_name: "Betty S.",
class_shortname: "rs",
date_completed: ...,
pass: false,
},
{
student_name: "Betty S.",
class_shortname: "lu",
date_completed: ...,
pass: true,
},
]
我想要的是一个完整的班级列表,为登录感兴趣的学生(Tom R.)
[
{
class_longname: "Underwater Volleyball",
class_shortname: "uv",
difficulty_1_10: 4,
instructor_name: "Mr. Smith",
special_needs: "snorkel and mask",
duration: "20 hours",
student_name: "Tom R.",
class_shortname: "uv",
date_completed: ...,
pass: true,
},
{
class_longname: "Yoga in the Mountains",
class_shortname: "ym",
difficulty_1_10: 7,
instructor_name: "Ms. Walrus",
special_needs: "yoga mat",
duration: "12 hours",
student_name: "Tom R.",
class_shortname: "ym",
date_completed: ...,
pass: true,
},
{
class_longname: "Roller skating disco dance party",
class_shortname: "rs",
difficulty_1_10: 4,
instructor_name: "Ms. Pineapple",
special_needs: "roller skates",
duration: "10 hours"
},
{
class_longname: "MongoDB database programming for beginners",
class_shortname: "mb",
difficulty_1_10: 4,
instructor_name: "Mr. Smith",
special_needs: "laptop",
duration: "4 hours"
},
{
class_longname: "Learn to Ride a Unicycle",
class_shortname: "lu",
difficulty_1_10: 4,
instructor_name: "Ms. apple",
special_needs: "bring your own unicycle",
duration: "15 hours"
}
]
现在我的Mongodb代码看起来像:
const result = await db
.collection('course_progress')
.aggregate([
{ $match: { student_name: "Tom R." } }, // actually a variable name, etc.
{ $unionWith: {coll: "classlist" } },
])
.toArray()
这是&;combine"这两个数组,有点像,但不是真的。我得到的是Tom的两个course_progress记录对象,然后是提供的所有五个类(数组中总共有七个对象)。Tom的course_progress中没有class_longname, instructor_name等记录…不知道如何合并、整合和清除这些数据。我知道我可以两次往返MongoDB服务器,得到两个结果数组,然后在客户端做一个forEach
来清理,但我真的希望在一个操作中在DB中做到这一点。
这在Mongodb是可能的吗?
你应该使用$lookup
来连接两个文档
db.course_progress.aggregate([
{
$match: {
student_name: "Tom R."
}
},
{
"$lookup": {
"from": "classlist",
"localField": "class_shortname",
"foreignField": "class_shortname",
"as": "classData"
}
},
{
"$unwind": "$classData"
},
{
"$project": {
class_longname: "$classData.class_longname",
class_shortname: "1",
difficulty_1_10: "$classData.difficulty_1_10",
duration: "$classData.duration",
instructor_name: "$classData.instructor_name",
special_needs: "$classData.special_needs",
date_completed: 1,
pass: 1,
student_name: 1
}
}
])
https://mongoplayground.net/p/hAkt1QsLbG0