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.
-
Wernfried commented
Did you try to use `--eval` option instead of a pipe? It works much better.