Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose schemas not picking up new properties

I have this mongoose schema, I added updated_by and created_by, but for some reason when I save models from client to server, those fields aren't visible:

   userSchema = mongoose.Schema({
            role: {
                type: String,
                enum: ['Admin', 'Owner', 'User']
            },
            username: {
                type: String,
                unique: true,
                required: true,
                validate: [validation.usernameValidator, 'not a valid username']
            },
            passwordHash: {
                type: String,
                required: true,
                validate: [validation.passwordValidator, 'not a valid password']
            },
            email: {
                type: String,
                unique: true,
                required: true,
                validate: [validation.emailValidator, 'not a valid email address']
            },
            firstName: {
                type: String,
                required: false
            },
            lastName: {
                type: String,
                required: false
            },
            registered_at: {
                type: Date,
                default: Date.now
            },
            created_by: {
                type: String,
                required: false
            },
            updated_by: {
                type: String,
                required: false
            },
            created_at: {
                type: Date,
                default: Date.now
            },
            updated_at: {
                type: Date,
                default: Date.now
            }
        },
        {
            autoIndex: false
        });

is this normally a problem? Do I have to somehow rebuild something with Mongoose or MongoDB in order for them to pick up the new properties on this model?

Of course, I did restart the mongoDB server, but that didn't do anything.

like image 883
Alexander Mills Avatar asked Sep 02 '25 04:09

Alexander Mills


2 Answers

In any case, if you save your User model, the fields with actual values shown in MongoDB will be the ones you set a value for yourself when saving the model OR the fields with a default value set in your userSchema.

So, just to clarify on this:

    address: { type: String, default: ' ' } 

will be shown in MongoDB with a value of ' ' unless you set a specific address when saving your User model.

But,

    address: String

will NOT be shown in MongoDB unless you set a specific address when saving your User model.

EDIT Thanks to Matthew for pointing it out, actually upsert behavior is indeed the following:

If upsert is true and no document matches the query criteria, update() inserts a single document.

like image 126
Pantelis Ieronimakis Avatar answered Sep 05 '25 02:09

Pantelis Ieronimakis


To anyone who is facing issue there schema is not updating in mongo please ensure first that the key which you are using is required or not if required then mark it as required true in schema if will show otherwise mongodb doesn't save that until unless you send it from Frontend

like image 30
Tarun Kantiwal Avatar answered Sep 05 '25 01:09

Tarun Kantiwal