Refactored mongoose code.