add mongodb support, moved function a bit.