some bug fixes and code cleanup for mondodb insertion