Skip to content
Advertisement

Add field not in schema with mongoose

I am trying to add a new field to a document, but this isn’t working:

Creating my UserModel prototype:

JavaScript

Then calling it

JavaScript

This successfully updates any field as long as it exists, but it won’t add any new one.

Advertisement

Answer

You can add and remove fields in schema using option { strict: false }

option: strict

The strict option, (enabled by default), ensures that values passed to our model constructor that were not specified in our schema do not get saved to the db.

JavaScript

And also you can do this in update query as well

JavaScript

You can check the documentations here

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement