skip to Main Content

Can I add new parameter in mongodb collection, which are not defined in mongoose schema?
Here is my schema

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);

And I want to add some parameters like EmailVerified and MobileNumberVerified
Here is my routes code which actually insert data in 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});
        }
    });

});

But in mongodb these fields are not present. I think mongoose doesn’t permit dynamically adding parameters.

2

Answers


  1. Adding paths to the model’s schema after the model has been compiled is unsupported. Please don’t do that.

    Login or Signup to reply.
  2. Mongoose allows dynamically adding parameters which are not present in schema. However it is disabled by default.

    You can enable it by using { strict: false } when defining model. See below code.

    const thingSchema = new Schema({ /* ... */ }, { strict: false });
    const thing = new Thing({ iAmNotInTheSchema: true });
    thing.save(); // iAmNotInTheSchema is now saved to the db!!
    

    Check out this article.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search