Inserting default values on update/overwrite
See original GitHub issuePrerequisites
- I have written a descriptive issue title
Mongoose version
6.6.5
Node.js version
16.17.0
MongoDB version
5.0.6
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
12.5.1
Issue
Hey there.
I’m currently writing a REST API builder for my application which also uses mongoose.
When implementing PUT, I’m now running into a problem: currently I get the document via findById()
, then call overwrite()
and markModified()
on it and then save()
. In these steps, really only those values get written into the document, which are given in overwrite()
, while I would expect default values from the defined schema to be set on attributes, which were not given in the PUT request.
Is this just missing, could we add an option for overwrite, is overwrite thought to be “forceful”, do i need the values to be set default mark as changed as well, is there any other option to achieve what I’m currently trying?
Thanks for your help, mongoose really eases a lot of my development.
Issue Analytics
- State:
- Created 10 months ago
- Comments:5
Top GitHub Comments
@EtzBetz yes,
applyDefaults()
only applies defaults on properties whose values are eithernull
orundefined
.Oh nice, I thought I searched through all the methods, but seems like I didn’t. From my test now,
applyDefaults
only applies defaults where are a value isn’t set, correct?