[query-optimization] Refactoring and huge optimization in MongoDB interaction