Express 如何从两个集合中查找查询

Express 如何从两个集合中查找查询,express,mongoose,Express,Mongoose,我正在从params获取studentid,并在获取注册信息后搜索注册模型,该注册信息包含我想在课程模型中通过courseID查找的studentid和courseID,以查找有关课程的详细信息并发回状态。我尝试这样做时不断收到错误。请帮助 课程模式 const mongoose = require("mongoose"); //Course Schema //Faculty can add courses to the DB const courseSchema = mongoose.

我正在从params获取studentid,并在获取注册信息后搜索注册模型,该注册信息包含我想在课程模型中通过courseID查找的studentid和courseID,以查找有关课程的详细信息并发回状态。我尝试这样做时不断收到错误。请帮助

课程模式

    const mongoose = require("mongoose");
//Course Schema
//Faculty can add courses to the DB
const courseSchema = mongoose.Schema({
  _id: mongoose.Schema.Types.ObjectId,
  courseID: { type: String, required: true },
  courseName: { type: String, required: true },
  courseDept: { type: String, required: true },
  courseDescription: { type: String, required: true },
  courseRoom: { type: String, required: true },
  waitListCap: { type: Number, required: true },
  courseTeam: { type: String },
  taughtBy:{ type: String }
});
module.exports = mongoose.model("Course", courseSchema);
    const mongoose = require("mongoose");
//Course Schema
//Faculty can add courses to the DB
const registrationSchema = mongoose.Schema({
  _id: mongoose.Schema.Types.ObjectId,
  courseID: { type: mongoose.Schema.Types.ObjectId, ref: "Course" },
  studentID:{ type: mongoose.Schema.Types.ObjectId, ref: "User" }

});

module.exports = mongoose.model("Registration", registrationSchema);

 router.get("/classregistration/:id", (req, res, next) => {
  Registration.find({
      studentID: req.params.id
    })
    .exec()
    .then(data => {
      data.map(single => {
        Course.find({
          courseID: single.courseID
        }).exec().then(course => {
          console.log('from course', course)
          const response = {
            course: course.map(singleData => {
              return {
                id: singleData._id,
                courseID: singleData.courseID,
                courseDept: singleData.courseDept,

              };
            })
          };

        })
        res.status(200).json(response);
      })


    })
    .catch(err => {
      console.log(err);
      res.status(500).json({
        error: err
      });
    });

})
注册模式

    const mongoose = require("mongoose");
//Course Schema
//Faculty can add courses to the DB
const courseSchema = mongoose.Schema({
  _id: mongoose.Schema.Types.ObjectId,
  courseID: { type: String, required: true },
  courseName: { type: String, required: true },
  courseDept: { type: String, required: true },
  courseDescription: { type: String, required: true },
  courseRoom: { type: String, required: true },
  waitListCap: { type: Number, required: true },
  courseTeam: { type: String },
  taughtBy:{ type: String }
});
module.exports = mongoose.model("Course", courseSchema);
    const mongoose = require("mongoose");
//Course Schema
//Faculty can add courses to the DB
const registrationSchema = mongoose.Schema({
  _id: mongoose.Schema.Types.ObjectId,
  courseID: { type: mongoose.Schema.Types.ObjectId, ref: "Course" },
  studentID:{ type: mongoose.Schema.Types.ObjectId, ref: "User" }

});

module.exports = mongoose.model("Registration", registrationSchema);

 router.get("/classregistration/:id", (req, res, next) => {
  Registration.find({
      studentID: req.params.id
    })
    .exec()
    .then(data => {
      data.map(single => {
        Course.find({
          courseID: single.courseID
        }).exec().then(course => {
          console.log('from course', course)
          const response = {
            course: course.map(singleData => {
              return {
                id: singleData._id,
                courseID: singleData.courseID,
                courseDept: singleData.courseDept,

              };
            })
          };

        })
        res.status(200).json(response);
      })


    })
    .catch(err => {
      console.log(err);
      res.status(500).json({
        error: err
      });
    });

})

你可以使用聚合,你会得到结果,请检查它,让我知道

Registration.aggregate([
  {
    $match : { studentID :req.params.id}
  },{
    $lookup:
    {
      from: "Course",
      localField: "courseID",
      foreignField: "courseID",
      as: "courses"
    }
  }
]).exec(function (err, res) {
  if (err) {
    response.json({
      error: true,
      message: err.message
    });
  }
  else if (res) {
    console.log(res)
    response.json({
      error: false,
      result: res,
      message: "Get courses  succesfully"
    });
   }
});

请提供课程模型和注册模型的模式,以便我可以帮助您编辑我的问题@NarendraChouhan谢谢您可以查看答案我已经这样做了谢谢您的帮助。但它一直给我一个错误错误:参数必须是聚合管道运算符我已经编辑了我的答案您能检查一下并让我看看吗知道,查询分支问题