Custom type with transformation upon read #12904
Labels
help wanted
help
This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Stale
Prerequisites
Mongoose version
6.8.2
Node.js version
6.17.1
MongoDB version
4.13.0
Operating system
Linux
Issue
I would like to manipulate a type
X
in my codebase, store it asY
format in the DB, and then get back aX
when I fetch.X => Y
can be done with a custom schema type'scast()
.However, I cannot find the way to do the opposite, i.e. deserialize
Y
intoX
upon fetching. The document I get from.find()
usesY
.A few workaround I've tried:
transform()
with.toObject()
. But.toObject
is only available in hydrated docs everytime, and I would prefer getting lean documents.X
. This is hardly scalable.mongoose.Type
andmongoose.SchemaType
. I tried to mimic what https://github.com/mongoosejs/mongoose-function is doing. But it did not work, and I see this library is not maintained since 2013, so I'm not sure it would work with current mongoose.This topic seems similar, but there are no solution proposed: #10423
The text was updated successfully, but these errors were encountered: