How does mongodb use Functions

  mongodb, question

For example, in mongoose, how to realize the following Functions?
clipboard.png

Running such logic on the database side is not recommended. Moreover, this is no different from defining such a function on NodeJS side. why must it be placed in MongoDB?
Describing the specific usage scenario may help us analyze the problem.