如何找到用mongoose的查询,如果用户是内部的一系列用户

0

的问题

我试图找到一种方法,来检查如果一个学生签署了一课程/s使用猫鼬。

我有这些架构:

课程架构:

    const mongoose = require("mongoose");
    const User = require("../models/User");

    const CourseSchema = new mongoose.Schema(
     {
    courseName: { type: String, required: true, unique: true },
    teacher: {
      teacherName: { type: String },
      teacherID: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "User",
      },
    },
    students: [
      {
        studentName: { type: String },
        studentID: {
          type: mongoose.Schema.Types.ObjectId,
          ref: "User",
        },
      },
    ],
    },
      { collection: "courses" },
     { timestamps: true }
     );

    module.exports = mongoose.model("Course", CourseSchema);

在这里我挽救的所有学生签署的课程内 学生学 阵列的对象。

学生模式:

const mongoose = require("mongoose");

const UserSchema = new mongoose.Schema(
  {
    username: { type: String, required: true, unique: true },
    email: { type: String, required: true, unique: true },
    password: { type: String, required: true },
    userType: {
      type: String,
      enum: ["student", "teacher"],
      default: "student",
    },
    isOnline: { type: Boolean, default: false },
  },
  { collection: "users" },
  { timestamps: true }
);
module.exports = mongoose.model("User", UserSchema);

现在我想做一个的查询将返回的一个列表中的课程的学生签署。

例如:

如果我有3个课程=[数学、英语、程序] 和学生有id=1人签署的(在学阵列)用于数学和英语,则查询将返回的数学和英语的课程。

我已经试过这个没有成功(得到null,但用户是在学阵列的对象):

router.post("/:id", async (req, res) => {
  try {
    // get user
    var user = await User.findOne({
      username: req.body.username,
      email: req.body.email,
      password: req.body.password,
    });
    // search user courses by user id.
    const coursesList = await Course.find({
      students: {
        $in: [{ studentID: user._id, studentName: user.username }],
      },
    });
    res.status(200).json(coursesList);
  } catch (err) {
    res.status(500).json(err.message);
  }
});
express javascript mongodb mongoose
2021-11-23 19:11:12
2
1

你可以做像这样:

const coursesList = await Course.find({ "students.studentID": user._id });

工作实例

2021-11-23 19:21:04
1

如果你只想要回匹配的用户,可以使用的聚合的管道。 现场演示在这里

数据库

[
  {
    "course": "Math",
    "students": [
      {
        studentID: "id_1",
        studentName: "Name 1"
      },
      {
        studentID: "id_2",
        studentName: "Name 2"
      }
    ]
  },
  {
    "course": "English",
    "students": [
      {
        studentID: "id_1",
        studentName: "Name 1"
      },
      {
        studentID: "id_3",
        studentName: "Name 3"
      }
    ]
  },
  {
    "course": "Programming",
    "students": [
      {
        studentID: "id_4",
        studentName: "Name 4"
      },
      {
        studentID: "id_5",
        studentName: "Name 5"
      }
    ]
  },
  
]

查询

db.collection.aggregate([
  {
    $unwind: "$students"
  },
  {
    $match: {
      "students.studentID": "id_1"
    }
  }
])

结果,

[
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "course": "Math",
    "students": {
      "studentID": "id_1",
      "studentName": "Name 1"
    }
  },
  {
    "_id": ObjectId("5a934e000102030405000001"),
    "course": "English",
    "students": {
      "studentID": "id_1",
      "studentName": "Name 1"
    }
  }
]
2021-11-23 20:27:18

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................