mongodbmongo-shellmongosh

Can mongosh (new MongoDB shell) produce the same output format than mongo (legacy MongoDB shell)?


The following command executed with legacy mongo shell:

mongo --quiet << EOF
  show dbs
EOF

provides this output

admin   0.000GB
config  0.000GB
local   0.000GB

However, the same command with new shell (mongosh)

$ mongosh --quiet << EOF
  show dbs
EOF

provides a slightly different ouput:

test>   show dbs
admin   40.00 KiB
config  36.00 KiB
local   40.00 KiB
test>

I have a set of old script using mongo that I want to migrate to mongosh but that slight difference in the output makes them break in some point. I wonder if I could avoid extra work adapting it configuring mongosh to provide output in the exact same way than mongo.

Is there some way of doing so, please?

Thanks in advance!

EDIT: the show db above is just an example. I'm looking for a general solution that covers in general any other case. For instance, this new one:

Legacy shell:

$ echo 'db.dropDatabase()' | mongo sample-db --quiet
{ "ok" : 1 }

New shell:

$ echo 'db.dropDatabase()' | mongosh sample-db --quiet
sample-db> db.dropDatabase()
{ ok: 1, dropped: 'sample-db' }
sample-db>

In fact, just the ability of removing the prompt part that output (test>, sample-db>, etc.) would help a lot.


Solution

  • There is eval parameter for that.

    Instead of

    $ echo 'db.dropDatabase()' | mongosh sample-db --quiet
    

    Do

    $ mongosh sample-db --quiet --eval 'db.dropDatabase()' 
    

    It outputs exactly

    { ok: 1, dropped: 'sample-db' }