Migrating anything, mongo or rocket.chat itself for a standalone deployment is more trickier to do with scripts as the environment may not be consistent (paths, dependencies and what not)
Also, unless you have a very strong requirement to do otherwise, you should run Rocket.Chat with docker (for small to mid sized deployments) or k8s for bigger deployments.
With that said, migrating mongo from 4.0 to 4.2 should be a matter of dumping and restoring. For that, we usually recommend to follow mongo docs itself.
If you want to use WiredTiger then you need to do this before you start the upgrade:
The main thing is that you need to make a backup using mongo dump mongodump --out=/dump
Then you need to start a fresh instance with the WiredTiger storage engine and do a restore mongorestore /dump
To upgrade version you need to install the new version of mongo over the existing one (so you use the same database files) and start it up. You can only go up 1 version at a time. (not a problem in your case)
Once it is started it will upgrade your db files. Then if you are happy with the upgrade you need to run: mongo 127.0.0.1/rocketchat --eval "db.adminCommand( { setFeatureCompatibilityVersion: \"4.2\" } )"
Please read the documentation on the mongodb website. I’ve only ever upgraded a single node.
And make a backup of your data before you start.