Mongo enhancements

This is a bunch of mongo related stuff I added. Hopefully the commit messages explain well enough.

Also fixes #1549