write_mongodb: fix potential NULL dereference