猫鼬参考现有文档保存文档?

时间:2020-09-15 13:00:25

标签: mongodb mongoose mongodb-query

我正在编写一个应用程序,使学生可以协作并跟踪他们花在完成任务上的时间。

我下面有两个模式

学生:

const studentSchema = new mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    studID: {
        type: String,
        min: 8,
        max: 8,
        required: true
    },
    units: [{
        type: mongoose.Schema.ObjectId,
        ref: 'Unit'
    }],
    fname: {
        type: String,
        required: true,
    },
    lname: {
        type: String,
        required: true
    }
});

任务:

const subtaskSchema = new mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    name: {
        type: String,
        required: true
    },
    estTime: {
        type: Number,
        default: 0
    },
    status: {
        type: String,
        required: true,
        default: 'Inactive'
    }
    ,
    students: [{                          //students involved in task
        type: mongoose.Schema.ObjectId,
        ref: 'Student'
    }],
    totalTime: {
        type: Number,
        default: 0
    }
});

如何查询引用现有学生的新子任务到数据库?

0 个答案:

没有答案