I need to search my Mongo database and get the average user for each month of the year. Is it more performant to do this directly with a database search using aggregation or would it be better for me to fetch all the data first and organize them with basic javascript functions like for, reduce, map ...?
CodePudding user response:
The short answer: MongoDB, because is written in c so will be more efficient than your JS function.
The long answer: You can read multiple articles or StackOverflow questions like:
- This baeldung article
- What are the pros and cons of performing calculations in sql vs. in your application question
- Which is Faster, MogoDB Aggregate or Self Implemented "For" Loop? question
- Mongo aggregation vs Java for loop and performance
In esence, a DB process is more optimized than a function in JS and is a better option use aggregation framework to do a math operation than JS code.