Simplify the mongo counting mechanism to a context manager
LMS-2909