added error checking for mongodb operations