Skip to content
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

Model.applyVirtuals(): apply schema virtuals to a POJO, similar to Model.applyDefaults() #14818

Open
2 tasks done
Vladyslav531 opened this issue Aug 19, 2024 · 4 comments
Open
2 tasks done
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class
Milestone

Comments

@Vladyslav531
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

Supporting virtual fields in aggregate will be good feature.

I know aggregate is server-side feature and virtual fields is client-side feature and aggregation results can change shape of document.
However, some aggregate results just extends document data and it is possible to add virtual fields to aggregate results, just I mentioned in motivation.
I think we can add hasVirtualFields to aggregate options which default is false and add virtual fields to aggregate results.

Motivation

I am using aggregate function for pagination.
Since aggregate doesn't support virtual fields, I need to hydrate each aggregation result and add virtual fields to each aggregation result manually.
It will be good to do in mongoose itself.

Example

UserModel.aggregate([
  // add pipelines
], {
  hasVirtualFields: true
});

So, we can add virtual fields like fullName to aggregate results.

@Vladyslav531 Vladyslav531 added enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class labels Aug 19, 2024
@windward-hive
Copy link
Contributor

"Virtuals are properties on Mongoose documents. If you use the lean option, that means your queries return POJOs rather than full Mongoose documents. That means no virtuals if you use lean()." Aggregate returns POJO, just like lean. Since virtuals are tied to mongoose documents, they won't work with aggregation results.

@Vladyslav531
Copy link
Author

I understand totally.
However, we can use hydrate to convert POJO to mongoose document and so, it is possible to add virtual fields to aggregation result.

@vkarpov15
Copy link
Collaborator

I think adding a Model.applyVirtuals() function similar to Model.applyDefaults() would be helpful. That way you can pass in an arbitrary object and have Mongoose apply the schema's virtuals to that object. @Vladyslav531 does that sound like it would fix your issue?

@Vladyslav531
Copy link
Author

Yes, that sounds great, @vkarpov15

@vkarpov15 vkarpov15 changed the title Support virtual fields in aggregate Model.applyVirtuals(): apply schema virtuals to a POJO, similar to Model.applyDefaults() Aug 21, 2024
@vkarpov15 vkarpov15 added this to the 8.7 milestone Aug 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class
Projects
None yet
Development

No branches or pull requests

3 participants