Factor out connecting to mongodb.