I see you almost always do 'echo $r' after invoking 'mongoCmd'.

With my proposed alternative to mongoCmd, you'd normally just call the function:

          mongoCmd 'whatever'

When the result from mongoCmd is necessary you'd have to catch it in a string:

         m=$(mongoCmd 'whatever')

It is just a proposal, but you'd save quite a few lines ...