Enhance the way middleware handle MongoDB connection