无法添加未在mongoose模式中定义的参数

时间:2017-02-03 08:09:02

标签: node.js mongodb express mongoose

我可以在mongodb集合中添加新参数吗?这些参数未在mongoose模式中定义? 这是我的架构

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这些领域并不存在。我认为mongoose不允许动态添加参数。

1 个答案:

答案 0 :(得分:0)

不支持在编译模型后添加模型架构的路径。请不要这样做。