-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Model.applyDefaults()
and Model.castObject()
#11945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'm not sure I understand what you're trying to do. If you have some plain data, you already have a lean document - a lean document is a POJO. I'm guessing what you're trying to do is apply defaults and type casting without hydrating a full document? |
Yeah exactly, I basically just want to apply the schema to my POJO and get back a POJO with all the defaults and type casting applied |
This is a good idea. It would be nice to have helper functions on models that apply defaults to a POJO and apply type casting to a POJO. Good for both internal refactoring as well as providing a way to pick and choose which parts of Mongoose you want to use. Similar to |
Model.applyDefaults()
and Model.castObject()
feat(model): add `applyDefaults()` helper that allows applying defaults to document or POJO
Prerequisites
Mongoose version
6.0.11
Node.js version
16
MongoDB version
4.6
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
No response
Issue
I have a use case where I want to be able to apply the Mongoose schema I have defined for a model to some plain data, without constructing the Mongoose document wrapper around it by doing
new Model
. The reason for this is to apply all the schema's defaults, datatype casting etc. without the CPU intensive task of constructing a full document with all the getters / setters and change tracking.Is there a way to do this in Mongoose? Essentially to provide it some JSON data and get back a lean version of the document I would otherwise get by doing
new Model
?Edit: I guess this question presumes that a "lean" query actually applies the Mongoose schema to the returned data, which I'm not actually sure is true. A better question then is basically "can I apply the schema defined by Mongoose to a piece of plain data without constructing a full document"?
The text was updated successfully, but these errors were encountered: