Migration to MongoDB

Lots of changes for the migration to MongoDB.