将猫鼬文档转换为其他类似模型
Cast Mongoose Document to other similar Model
所以我的应用程序有以下布局:
UserSchema(包含用户名和密码)
var UserSchema = new mongoose.Schema({
firstName: String,
lastName: String,
email: String,
username: String,
password: String
}, options);
UserSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model("User", UserSchema);
具有附加字段的 StudentSchema(用户上的鉴别器())
var StudentSchema = new mongoose.Schema({
indexNumber: String,
courses: [{
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "Course"
},
entryDate: {
type: Date,
default: Date.now
}
}]
}, options);
module.exports = User.discriminator("Student", StudentSchema);
TeacherSchema(用户上的鉴别器())带有附加字段
var TeacherSchema = new mongoose.Schema({
degree: String,
courses: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Course"
}]
}, options);
module.exports = User.discriminator("Teacher", TeacherSchema);
我正在使用通行证本地策略 User.register()。
router.post("/register", function(req,res) {
var newUser = new User({
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
username: req.body.username
});
User.register(newUser, req.body.password, function(err, user) {
if(err) throw err;
passport.authenticate("local")(req, res, function() {
req.flash("success", "Welcome " + user.username + ".");
res.redirect("/");
});
});
});
现在所有这些都在数据库中的同一个集合 users 下(因为鉴别器)
我如何在用户注册后,"promote"他给学生或老师,这样我就可以在他的对象中包含这些附加字段?
尝试在用户模型上使用 update
命令将 __t
设置为 "Teacher" 或 "Student"。我认为默认的鉴别器键是 __t
所以你需要这样的东西:
user.update({$set: {__t: "Student" } }, callback)`
然后你需要再次用鉴别器拉它来得到你的字段:
const Student = User.discriminator("Student", StudentSchema);
Student.findById(id, callback)
所以我的应用程序有以下布局:
UserSchema(包含用户名和密码)
var UserSchema = new mongoose.Schema({ firstName: String, lastName: String, email: String, username: String, password: String }, options); UserSchema.plugin(passportLocalMongoose); module.exports = mongoose.model("User", UserSchema);
具有附加字段的 StudentSchema(用户上的鉴别器())
var StudentSchema = new mongoose.Schema({ indexNumber: String, courses: [{ id: { type: mongoose.Schema.Types.ObjectId, ref: "Course" }, entryDate: { type: Date, default: Date.now } }] }, options); module.exports = User.discriminator("Student", StudentSchema);
TeacherSchema(用户上的鉴别器())带有附加字段
var TeacherSchema = new mongoose.Schema({ degree: String, courses: [{ type: mongoose.Schema.Types.ObjectId, ref: "Course" }] }, options); module.exports = User.discriminator("Teacher", TeacherSchema);
我正在使用通行证本地策略 User.register()。
router.post("/register", function(req,res) {
var newUser = new User({
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
username: req.body.username
});
User.register(newUser, req.body.password, function(err, user) {
if(err) throw err;
passport.authenticate("local")(req, res, function() {
req.flash("success", "Welcome " + user.username + ".");
res.redirect("/");
});
});
});
现在所有这些都在数据库中的同一个集合 users 下(因为鉴别器)
我如何在用户注册后,"promote"他给学生或老师,这样我就可以在他的对象中包含这些附加字段?
尝试在用户模型上使用 update
命令将 __t
设置为 "Teacher" 或 "Student"。我认为默认的鉴别器键是 __t
所以你需要这样的东西:
user.update({$set: {__t: "Student" } }, callback)`
然后你需要再次用鉴别器拉它来得到你的字段:
const Student = User.discriminator("Student", StudentSchema);
Student.findById(id, callback)