refactored code for mongoose optimization.