Mongosh script output is different from mongo and mongosh quiet option does not work when running it via kubectl exec or docker exec

We are currently upgrading from 4.4 to 5.0. Our Installation scripts have a lot of mongo commands that runs and evaluates the output. Due to the deprecation of mongo, we tried to migrate our scripts to use mongosh. However, we are running into issues due to the output mismatch between two.

Here is the difference between two outputs. As you can see below, mongo returns the results which is the number of documents from a collection, where as mongosh is returning connectivity strings along with some other things. I tried various options to figure out hwo to get it back to the original mongo. We have a lot of scripts that use mongo + quiet + eval. I am hoping there is an easy way to fix this issue.
How do I go about making mongosh not print the connectivity information in the result. As you

>>docker exec -it d58a07d2fbe1 mongo --quiet -host  testmongosh.js  | cat -v

>>$ docker exec -it d58a07d2fbe1 mongosh --quiet -host  testmongosh.js  | cat -v
^[]0;mongosh mongodb://^G^[[1G^[[0J ^[[1G^[[33m0^[[39m^M

0^M ( all our code is scripted to expect just return from js) - And mongosh is returning : ^0;mongosh mongodb://^G^[[1G^[[0J ^[[1G^[[33m0^[[39m^M

Mongo Server and Mongosh versions:

root@d58a07d2fbe1:/# mongosh
Current Mongosh Log ID: 657bd6d21f9133d7393d36b5
Connecting to: mongodb://
Using MongoDB: 5.0.22
Using Mongosh: 2.0.2

For mongosh info see:

Javascript file: testmongosh.js (to show the issue)

var mongo = db.getMongo()
bl_db = mongo.getDB("test-service")

Adding this: I tried mongosh from within the container with --quiet flag and it seems to be doing the right thing. I am not sure where the issue is. (probably a term issue). Not sure why it does not exist with mongo but with mongosh.

docker exec -it d58a07d2fbe1 bash -c 'mongosh --quiet  --file testmongosh.js | cat -v'

seems to return it correctly. Keeping this open, because I would like to get mongo’s view point.

@Parvathi_Nair1 Hi, and thank you for the post!

I think there might be some misunderstanding here regarding what the docker command line flags do exactly. When you run docker exec with the -t option, you are explicitly telling it to run the command in a (pseudo-)TTY, i.e. as if it was interacting with a real terminal that understands all normal terminal commands. This is probably not something you want to do in scripted usage, though.

In this case, the connection string is part of the output because mongosh uses terminal escape sequences to set the terminal title while it is running, but of course only when it detects that its output is connected to a terminal.

If you do not want this behavior because you want to use the command output programmatically, then dropping the -t (and probably also the -i unless you are actually using stdin in that way) is absolutely the right thing to do, regardless of whether you are running mongo, mongosh, or any other script inside the container, i.e. docker exec d58a07d2fbe1 mongosh --quiet --file ... should be working just fine.

I hope this is helpful!

Thanks. I did think about this - and just forgot to try it. Was just trying with existing scripts (which does have kubectl exec -it in them) and went to figure out why mongo and mongosh behaves differently.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.