NodeJS Mongoose在一个查询中从多个集合中获取结果

wfsdck30  于 2022-10-04  发布在  Go
关注(0)|答案(0)|浏览(155)

我对NodeJS和MongoDB非常陌生,我有3个集合,一个用于章节,一个用于讲座,一个用于资产每个课程都有章节,每个章节都有一组讲座,每个讲座都有资产,所以我想通过CourseID来获得章节,里面的章节来获得它的讲座,并在每一节课程中获得资产

课程:

const mongoose = require('mongoose');
var Double = require('@mongoosejs/double');

const courseSchema = new mongoose.Schema({
    title: {
        type: String,
        required: true,
    },
    requirements: {
        type: String,
    },

    code: {
        type: String,
        required: true,
    },
    coverPhoto: {
        type: String,
        required: false,
    },

    description: {
        type: String
    },

    instructor:{
        type:mongoose.Schema.Types.ObjectId,
        ref:'User',
        required:true
    },

    category:{
        type:mongoose.Schema.Types.ObjectId,
        ref:'Category',
        required:true
    },

    learns: [{
        type: String
    }],
    subCategory:{
        type:mongoose.Schema.Types.ObjectId,
        ref:'SubCategory',
        required:true
    },

    isCoaching: {
        type: Boolean,
        default: false,
    },

    isFree: {
        type: Boolean,
        default: false,
    },

    price: {
        type: Double,
        default: 0,
    },
    rating: {
        type: Double,
        default: 0,
    },
    isPublished: {
        type: Boolean,
        default: false,
    },

    dateCreated: {
        type:Date,
        default:Date.now,
    },
});

exports.Course = mongoose.model('Course', courseSchema);
exports.courseSchema = courseSchema;

第二章:

const mongoose = require('mongoose');

    const chapterSchema = new mongoose.Schema({
        course:{
            type:mongoose.Schema.Types.ObjectId,
            ref:'Course',
            required:true
        },
        title: {
            type: String,
            required: true,
        },
        sort_order: {
            type: Number,
            default: 1,
        },
        is_published: {
            type: Boolean,
            default: true,
        },

    });
    exports.Chapter = mongoose.model('Chapter', chapterSchema);
    exports.chapterSchema = chapterSchema;

讲座:

const mongoose = require('mongoose');

const lectureSchema = new mongoose.Schema({
    chapter:{
        type:mongoose.Schema.Types.ObjectId,
        ref:'Chapter',
        required:true
    },

    title: {
        type: String,
        required: true,
    },
    sort_order: {
        type: Number,
        default: 1,
    },

    is_published: {
        type: Boolean,
        default: true,
    },

});

exports.Lecture = mongoose.model('Lecture', lectureSchema);
exports.lectureSchema = lectureSchema;

资产:

const mongoose = require('mongoose');

const assetSchema = new mongoose.Schema({
    lecture:{
        type:mongoose.Schema.Types.ObjectId,
        ref:'Lecture',
        required:true
    },
    title: {
        type: String,
        required:true
    },
    asset_type: {
        type: String
    },
    description: {
        type: String,
        require:true
    },

    file_url: {
        type: String,
        require:true
    },
    page_number: {
        type: Number,
        default:1
    },
    time_estimation: {
        type: String,
        require:true
    },

    is_external: {
        type: Boolean,
        default: false,
    },

    is_published: {
        type: Boolean,
        default: true,
    },

});

exports.Asset = mongoose.model('Asset', assetSchema);
exports.assetSchema = assetSchema;

获取课程章节

router.get(`/`, async (req, res) => {
    let course_filter = {};
    if (req.query.course) {
        course_filter = {course:req.query.course};
    }
    const chapterList = await Chapter.find(course_filter).populate('lecture').sort('sort_order');
    if (!chapterList) {
        res.status(500).json({ success: false });
    }
    res.send(chapterList);
});

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题