-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
toJSON or reverseTranslateAliases function to return alias keys #7952
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
Adding the following to the mongoose schema options solves the issue. It removes all the raw keys and only aliases are left. {
toJSON: {
virtuals: true,
transform: (doc: any, ret: any, opts: any) => {
Object.keys(doc.schema.obj)
.filter(k => doc.schema.obj[k].alias)
.forEach(ak => delete ret[ak]);
}
}
} Maybe a plugin can help? |
This is by design. The primary purpose of aliasing keys is to reduce storage in the database and network overhead between Node.js and MongoDB by shortening the keys when they're stored in the database, and converting the aliased keys back to long-form keys in Node.js. |
If it's by design, then why doesn't it work as designed? |
@klemensz if it doesn't work as designed, please open up a new issue and follow the issue template. |
Do you want to request a feature or report a bug?
What is the current behavior?
If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
toObject
ortoJSON
should return the aliased keys instead of the raw keys. Or a custom function likereverseTranslateAliases
can be implemented.Related to #5184
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
mongoose: 5.6.2
node: 12.0.0
npm: 6.9.0
The text was updated successfully, but these errors were encountered: