The new mongosh mangles the output of piped in commands
With the old mongo shell, you could set up a shell file (called something like mongo_cmd.sh)containing the path to the executable and all the command line switches you wanted like this:
/path/to/mongo --norc --quiet --port 26011
and then in your other script files pipe commands into an invocation of mongocmd.sh like the following:
```
CMDFILE=./mongo_cmd.sh
STATS=$(echo 'db.stats().ok' | $CMDFILE)
DBVERSION=$(echo 'db.version()' | $CMDFILE)
echo "db.stats().ok output:" $STATS
echo "db.version() output:" $DBVERSION
```
and the output would be this:
db.stats().ok output: 1
db.version() output: 6.0.5
But if you replace mongo with the new shell mongosh in the above, the output becomes garbles with the mongosh shell prompt appearing both before and after each command output:
db.stats().ok output: Enterprise s1 [direct: primary] test> 1 Enterprise s1 [direct: primary] test>
db.version() output: Enterprise s1 [direct: primary] test> 6.0.5 Enterprise s1 [direct: primary] test>
The new mongosh shell should either be fixed to replicate the old shell behaviour, or it should be enhanced with a new command line switch --legacy that can be used to revert the behaviour for that session.
![](https://secure.gravatar.com/avatar/fa16964955e56817a83a00191fa30c77?size=40&default=https%3A%2F%2Fassets.uvcdn.com%2Fpkg%2Fadmin%2Ficons%2Fuser_70-6bcf9e08938533adb9bac95c3e487cb2a6d4a32f890ca6fdc82e3072e0ea0368.png)
-
Wernfried commented
Did you try to use `--eval` option instead of a pipe? It works much better.