Aggregation on a single document vs logic in the view function

Hi, I’m working on a web app where loading the user’s data model has a lot of dependent fields. Loading the user’s profile page requires calculating these values. I am currently using an aggregation pipeline what uses a $match stage and works with a single document (of that user) in the pipeline to calculate the other fields. This way, the code inside the view (or controller) function of the web app is very clean. Is this a good practice ?
Should I fetch the document and do the logical calculation inside the view function ?
What are some tips on when and when not to use the aggregation ?

Hi,

I would say you shouldn’t do that in the view, all that logic should be in a controller. Regarding the use of the aggregation framework, just use it if you need it, indeed if you can do those calculations using the aggregation framework I would go that way, it likely would be faster than processing the resulting documents in the application.

Hope this helps.

José Carlos

1 Like

Thanks a lot Carlos. This really helped.
I should correct myself, I meant controller when I said view (or controller)
It’s just that in some frameworks the controller is called view and the view is called template.
That was a tiny error on my part.

You are welcome. Thanks for pointing that some frameworks use the term view to refer to controllers, I find that a little bit odd … :thinking:

José Carlos