Refactor and support Mongoose as the driver

Refactor a bit so we can use either mongoose or the native mongodb driver. Mongoose lets us take advantage of connection pooling and nearly supports the same interface.

Refactor some more, so we're not nesting functions that could be declared separately. And also so that @hampelm has to read every line to make sure I didn't do anything crazy. Your welcome.

/cc @hampelm