1
我可以在mongodb集合中添加新的參數,這些參數未在貓鼬模式中定義? 這裏是我的架構無法添加未在貓鼬模式中定義的參數
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UsersSchema = new Schema({
FirstName : {
type : String
},
LastName : {
type : String
},
ProfileName : {
type : String
},
EmailID : { //This may actually take Phone Number depending on user account.
type : String,
required : true
},
Login : {
type : { enum : ['Facebook', 'Gmail', 'Custom'] },
required : true
},
ContactNumber :
{
type : Number
},
Address : { //Add Geo co-ordinates location later.
type : {}
},
ProfilePic : {
type : String //URL to image
},
Birthday : {
type : {}
},
Gender : {
type : { enum : ['Male', 'Female']}
},
CreatedDate : {
type: Date,
default: Date.now
},
ModifiedDate : {
type: Date,
default: Date.now
},
LastLogin : {
type: Date,
default: Date.now
}
});
module.exports = mongoose.model('Users', UsersSchema);
而且我想添加一些參數,如EmailVerified和MobileNumberVerified 這裏是我的路線代碼實際上在MongoDB中
router.post('/api/signup',function(req,res){
console.log("Registering user");
var user = new Users();
user.FirstName = req.body.FirstName;
user.LastName = req.body.LastName;
user.EmailID = req.body.EmailID;
user.Login = "Custom";
user.Password = req.body.Password;
user.ProfileName = req.body.FirstName + " " +req.body.LastName;
// user.Birthday =
user.Address = req.body.Address;
user.Gender = req.body.Gender;
user.EmailVerified = false; // dynamic parameter
user.MobileNumberVerified = false; // dynamic parameter
// user.ContactNumber = req.body.ContactNumber;
user.save(function(err,user){
if(err){
console.log(err);
res.json(err);
}else{
console.log("User Registered");
res.json({result : 1});
}
});
});
插入數據,但在MongoDB中,這些字段不存在。我認爲貓鼬不允許動態添加參數。
您應該使用貓鼬虛擬http://mongoosejs.com/docs/2.7.x/docs/virtuals.html – styopdev
謝謝@styopdev。 'Users.set(「EmailVerified」,false);'對我來說工作正常。 –