Add error handling when inserting into mongodb