Skip to content

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

Closed
abhisekp-bac opened this issue Jul 2, 2019 · 4 comments
Closed
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@abhisekp-bac
Copy link

Do you want to request a feature or report a bug?

feature

What is the current behavior?

findOneAndUpdate query toJSON doesn't return the aliased keys. Rather it returns the original keys.

If the current behavior is a bug, please provide the steps to reproduce.

What is the expected behavior?
toObject or toJSON should return the aliased keys instead of the raw keys. Or a custom function like reverseTranslateAliases 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

@abhisekp-bac
Copy link
Author

abhisekp-bac commented Jul 2, 2019

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?

@vkarpov15 vkarpov15 added this to the 5.x Unprioritized milestone Jul 3, 2019
@vkarpov15 vkarpov15 added enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature and removed enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature labels Jul 3, 2019
@vkarpov15 vkarpov15 removed this from the 5.x Unprioritized milestone Jul 3, 2019
@vkarpov15
Copy link
Collaborator

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.

@vkarpov15 vkarpov15 added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Jul 3, 2019
@klemensz
Copy link

If it's by design, then why doesn't it work as designed?

@vkarpov15
Copy link
Collaborator

@klemensz if it doesn't work as designed, please open up a new issue and follow the issue template.

@Automattic Automattic locked as too heated and limited conversation to collaborators Mar 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

3 participants