Mongoose schemas do not collect 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, these fields are not 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 usually a problem? Do I have to somehow rebuild something with Mongoose or MongoDB so that they can pick up the new properties of this model?

Of course I restarted the mongoDB server but did nothing.

+3


source to share


1 answer


In any case, if you save the user model, the fields with the actual values ​​displayed in MongoDB will be the ones you set for yourself when you saved the model, or in the fields with the default value set in your custom schema.

So, just to clarify on this:

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

      

will show up in MongoDB with the value '' unless you specify a specific address when saving your user model.

But,



    address: String

      

will show up NOT in MongoDB unless you specify a specific address when saving your user model.

EDIT Thanks to what Matthew pointed out to this, the behavior actually looks like this:

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

+1


source







All Articles