db handling

Works with flat file if the mongo db is not available.