猫鼬人口未归还所有田地

时间:2017-10-12 22:37:08

标签: javascript node.js mongodb mongoose

所以我有: 的 UserSchema

var UserSchema = new mongoose.Schema({
    firstName: String,
    lastName: String,
    image: {
        type: String,
        default: "http://via.placeholder.com/250x200"
    },
    email: {
        type: String,
        required: [true, "Email missing."],
        unique: true
    },
    kind: {
        type: String,
        enum: ["Admin", "Teacher", "Student"],
        default: "Student"
    },
    isAdmin: {
        type: Boolean,
        default: false
    },
    isPromote: {
        type: Boolean,
        default: false
    },
    username: {
        type: String,
        required: [true, "Username missing."],
        unique: true
    },
    password: String
}, options);

UserSchema.plugin(passportLocalMongoose);

module.exports = mongoose.model("User", UserSchema);

StudentSchema

var options = {discriminatorKey: 'kind'};
var StudentSchema = new mongoose.Schema({
    indexNumber: String,
}, options);

module.exports = User.discriminator("Student", StudentSchema);

CourseSchema

var CourseSchema = new mongoose.Schema({
    code: {
        type: String,
        unique: true
    },
    name: String,
    description: String,
    image: {
        type: String,
        default: "http://via.placeholder.com/250x200"
    },
    data: [{
        title: String,
        content: String
    }],
    teachers: [{

            type: mongoose.Schema.Types.ObjectId,
            ref: "Teacher"

    }],
    students: [{
        id: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "Student"
        },
        entryDate: {
            type: Date,
            default: Date.now
        }
    }]
});

在我的数据库中,我有以下学生(使用MongoDB Compass来显示): 2 Student Documents

另外,我有老师Teacher Document

这个课程

Course Document

现在在课程展示页面上,我想让学生注册和老师。我是这样的:

Course.findById(req.params.id)
.populate("students.id")
.populate("teachers")
.exec( function(err, foundCourse) {...}

我只能得到这个:

students: [
  { _id: 59dbd1259df328279cd5d588, entryDate: 2017-10-12T22:29:48.297Z },
  { _id: 59dc07088c6fd636188d87c5, entryDate: 2017-10-12T22:29:48.297Z }
],
teachers: [
  { _id: 59dbc34832b0041b44a7f648, isPromote: false, isAdmin: false, kind: 'Teacher', image: 'http://via.placeholder.com/250x200' }
]

请注意:

  • 对于学生,我只回到ref _ids,没有填充的字段。
  • 对于老师我没有得到一些字段,如firstName,lastName,username等(教师也是用户的鉴别者)

我对填充查询的期望是获取所有字段,例如,学生[0]应该如下所示:

{ _id: 59dbd1259df328279cd5d588, isPromote: false, isAdmin: false, kind: 'Student', image: 'http://via.placeholder.com/250x200', firstName: 'ss', lastName: 'ss', username: 'ss', entryDate: 2017-10-12T22:29:48.297Z }

有谁能告诉我如何获得这些领域,如何填充正确的方式?

0 个答案:

没有答案