我收集了主题,课程,级别,顺序和课程。在下面提供每个架构。
"Topics":{
course: {type: mongoose.Schema.ObjectId, ref: 'Courses'},
"topicsName": String,
"topicsURL": String
} // Note here Course is just an object not an Object Array.
"Courses":{
levels: [{
type: mongoose.Schema.ObjectId, ref: 'Levels'
}]
}
"Levels":{
orders: [{
type: mongoose.Schema.ObjectId, ref: 'Orders'
}]
}
"Orders":{
lessions: [{
type: mongoose.Schema.ObjectId, ref: 'Lessions'
}]
}
"Lessions":{
_id: mongoose.Schema.ObjectId, // Auto generated Field by Mongoose
"name": String,
"description": String
}
现在,我拥有“课程ID”之一。使用Lessions ID,我需要检索主题名称和主题URL。
下面的猫鼬代码,我曾用来获取给定课程ID的主题详细信息。
let topic = {};
const order = await Orders.find({ 'lessions': req.params.id});
if (order.length) {
const level = await Levels.find({ 'orders': order[0]._id });
if(level.length) {
const courses = await Courses.find({ 'levels': level[0]._id });
if(courses.length) {
const topics = await Topics.find({ 'course': courses[0]._id });
if(topics.length) {
topic._id = topics[0]._id;
topic.name = topics[0].name;
}
}
}
}
在Mongo / Mongoose中,如何通过运行单个查询来获取主题架构?