Refactoring mongo implementation to fix unification query bug