कैसे करने के लिए मिल का उपयोग कर नेवला क्वेरी यदि उपयोगकर्ता के अंदर एक सरणी के साथ उपयोगकर्ताओं को

0

सवाल

मैं कोशिश कर रहा हूँ करने के लिए एक रास्ता खोजने अगर जाँच करने के लिए एक छात्र है पर हस्ताक्षर किए के लिए एक पाठ्यक्रम/एस का उपयोग कर mongoose.

मैं इन स्कीमा:

पाठ्यक्रम स्कीमा:

    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 पाठ्यक्रम = [गणित, अंग्रेजी, प्रोग्रामिंग] और एक छात्र के साथ आईडी = 1, जो पर हस्ताक्षर किए (में छात्रों सरणी) के लिए गणित और अंग्रेजी में है, तो क्वेरी वापस आ जाएगी गणित और अंग्रेजी के पाठ्यक्रमों.

मैंने कोशिश की है बिना यह सफलता (अशक्त हो रही है, लेकिन उपयोगकर्ता में छात्रों वस्तुओं की सरणी):

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ý
..................................................................................................................

इस श्रेणी में लोकप्रिय

लोकप्रिय सवाल इस श्रेणी में