Major refactoring of the MongoDB queries to make the code simpler and lock operations significantly more efficient